Home > DeveloperSection > Articles > View in ASP.NET MVC

View in ASP.NET MVC


ASP.NET MVC ASP.NET MVC 
Ratings:
0 Comment(s)
 4181  View(s)
Rate this:

View in ASP.NET MVC

View is an ASPX page without having a code behind file. All page specific HTML generation and formatting can be done inside view. We can use inline code (server tags) to develop dynamic pages. A request to view (ASPX page) can be made only from a controller’s action method.

It is recommended that before creating View add Controller and Model in an application but it’s not compulsory.

We can add View in an MVC application:
Right click Home folder that is nested with in the View folder
à Select Add à Select View.

View in ASP.NET MVC

When you select View option, Add View dialog box will open.
1) Give the View Name as you created in a HomeController or give Index() if nothing is
    provided.
2) Here we create our view as a strongly-typed view, therefore check Create a strongly-
    typed view
option and add a Model class from the Dropdown list in a View.
    Strongly typed view is created when we want to use the functionality of particular model.
    If we select normal view then we cannot use the functionality of model. In that case, we 
    can use Controller properties (ViewData, ViewBag and TemplateData) to send the 
    dynamic data in that view.

3) Click on Add button to add a view successfully.

View in ASP.NET MVC

Write some part of code in view as written below, so you can identify view page easily.

<html>

<head runat="server">

    <title>Index</title>

</head>

<body>

    <div>

        <center>

        <h2>This is my First View Page</h2>

        </center>

    </div>

</body>

</html>

 

After creating a view, also create a Controller class in order to run an application.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace FisrtMVCApp.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

        public ActionResult Index()

        {

            return View();

        }

    }

}

 

A view is a standard (X)HTML document that can contain scripts. You use scripts to add dynamic content to a view.

You use the script delimiters <% and %> to mark the beginning and end of a script. This script is written in C#. It displays the current date and time by calling the Response.Write() method to render content to the browser. The script delimiters <% and %> can be used to execute one or more statements.

 

<html>

<head runat="server">

    <title>UserPage</title>

</head>

<body>

    <div>

        The current date and time is

        <% Response.Write(DateTime.Now);%>

    </div>

</body>

</html>

You can use HTML Helpers to generate standard HTML elements such as textboxes, links, dropdown lists, and list boxes.
All of the HTML Helpers methods are called on the Html property of the view. For example, you render a TextBox by calling the Html.TextBox() method.

        <label for="UserID">User ID :</label>

        <br />

        <%= Html.TextBox("UserID") %>

 


Don't want to miss updates? Please click the below button!

Follow MindStick