Asp .Net MVC - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.Net MVC View

ASP.Net MVC View

What is the view in MVC?

shape Description

The View is one of the components in the MVC architecture.The View acts like a User interface in ASP.Net web forms and is communicated with the end user through the controller. The controller contains the views and act as a mediator between the end user and the view. In general, ASP.Net user requests are mapped to web pages from the browsers. Whereas in case of MVC, user requests from the browser are not mapped to view. They are mapped to "Controller's Action Methods".

Types of views in ASP.Net MVC

shape Description

ASP.Net MVC supports 3 types of views.

Default Extensions of the view

shape Image

Adding the view in MVC?

shape Description

Before adding the view,one have to add the controller. To know about "How to add the controller?", read the previous chapter and for now, Follow the below steps to add the view.

shape Step 1

Go to the Solution Explorer. Open the folder named as Views. Delete the folders which is seen under the Views folder. Right click on the View, that's going to bring a popup menu. Select Add->View as shown in the below figure.

shape Step 2

By clicking on the View, that's going to bring a new window. Where one can give their own View name, and then uncheck the checkbox Use a layout. Click on Add button as shown in the below figure.

shape Step 3

Then, the code will be generated as shown in the following window.

shape Step 4

Now, write the code as follows to print a simple statement. [html] @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width"> <title>HomPage</title> </head> <body> <div> Welcome to Asp.net MVC 5. <!-- write this statement to a simple print statement --> </div> </body> </html> [/html]

shape Step 5

Now, Click on the controller, the one which have been created previously as shown in the below figure.

shape Step 6

Now, write the code as follows in the controller. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace HelloMVC.Controllers { public class HelloMVCController : Controller { // GET: HelloMVC public ActionResult Index() { return View(); } public ActionResult GotoHome() //GotoHome will invokes the Homepage { return View("HomePage"); } } } [/csharp]

shape Step 7

Press f5 to run the application. The following window will appear in the browser.

shape Step 8

An error will be occurred as shown in the above figure,because of not creating any index file. Type the following URL in the address bar and press Enter. Then, it will print the statement which have been given previously as shown in the below figure.

View Page Class

shape Description

In general, a view page is used to perform the required design for the user interface element within the ASP.Net MVC. The view, which one create's is a class and will contain the following additional properties.

AJAX

shape Description

Ajax is used to set or get Ajax helper class, and used to render HTML in Ajax scenario. AjaxHelper Class includes methods which support Client-Side Functionality. Like as
  • Asynchronous Forms
  • Rendering Links
AjaxHelper Class also supports Asynchronous Partial Page Updates. The following are the some methods supported by AjaxHelper Class.

Model

shape Description

Model Property is used to get the model property of the associated view data dictionary object.

TempData

shape Description

TempData Used to get the Temporary Data required to pass to the view. The Namespace for the TempData is System.Web.MVC.TempDataDictionary Refer the later chapters to know about the TempData briefly.

URL

shape Description

URL is used to set or get the URL for the render page.

ViewBag

shape Description

ViewBag is similar to ViewData but ViewBag supports Dynamic DataType. Refer the later chapters to know about the ViewBag briefly.

View Context

shape Description

View Context is used to set or get the information and used to render the view. Here, the information means Form Context, TempData, ViewData Etc.

ViewData

shape Description

ViewData is used to set or get the Key and Value pair in the form of a dictionary and used to pass the data from the controller to the view. Refer the later chapters to know about the ViewData briefly.

Write

shape Description

Write property is used to get the Text Writer and used to render the view in response. Write property is used to write the Html output.

ASP.Net MVC 5 Strongly Typed View

shape Description

A Strongly Typed View is a view which bind with any model. In general, View inherits from the Viewpage whereas Strongly Typed View inherits from the Viewpage<Tmodel>. Where Tmodel is a type of model. The Strongly Typed View has the following Advantages.

shape Example

Follow the below steps for creating an application.

shape Step 1

Go to solution Explorer-> Right-click on the Models folder and Add->class. Give the class name as Employee. Write the following code under the class. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace StronglyTypedView.Models { public class Employee { public int EmpId { set; get; } public string EmpName { set; get; } public string EmpDesignation { set; get; } public DateTime EmpDOJ { set; get; } public double EmpSal { set; get; } public int EmpDeptNo { set; get; } } } [/csharp]

shape Step 2

Go to Solution Explorer->Controllers->HomeController.cs and Add the below namespace in HomeController.cs [csharp]using StronglyTypedView.Models;[/csharp] Write the following code under the HomeController.cs [csharp] public ActionResult EmployeeDetails() { Employee obj = new Employee(); obj.EmpId=1; obj.EmpName="James"; obj.EmpDesignation = "Developer"; obj.EmpDOJ = DateTime.Now; obj.EmpSal = 70000; obj.EmpDeptNo = 10; return View(obj); } [/csharp]

shape Step 3

Go to Solution Explorer-> Views->Right Click on View Folder->Add->View. Give the view name as EmployeeDetails and write the following code under the EmployeeDetails.cshtml [html] @{ ViewBag.Title = "EmployeeDetails"; } <h2> Welcome to SPLessons Tutorials> </h2> <table> <tr> <td>@Html.Label("lblId", "Employee Id")</td> <td>@Html.TextBox("txtId")</td> </tr> <tr> <td>@Html.Label("lblName", "Employee Name")</td> <td>@Html.TextBox("txtName")</td> </tr> <tr> <td>@Html.Label("lblDes", "Designation")</td> <td>@Html.TextBox("txtDes")</td> </tr> <tr> <td>@Html.Label("lblDOJ", "Employee DateOfJoining")</td> <td>@Html.TextBox("txtDOJ")</td> </tr> <tr> <td>@Html.Label("lblSalary", "Employee Salary")</td> <td>@Html.TextBox("txtSal")</td> </tr> <tr> <td>@Html.Label("lblDeptNo", "Department Number")</td> <td>@Html.TextBox("txtDeptNo")</td> </tr> <tr> <td colspan="2"> <input type="submit" value="SUBMIT" name="btnSubmit" /></td> </tr> </table> [/html]

shape Step 4

Change the Action name in the RouteConfig.cs Press f5 to run the application. Then, the following output will appear in the browser as follows.