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.
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.
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>
<label for="UserID">User ID :</label>
<br />
<%= Html.TextBox("UserID") %>
Leave Comment