Loading

ASP.NET MVC

What is Strongly Typed Views in ASP.NET MVC Application?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

In this Video, I am going to discuss Strongly Typed Views in ASP.NET MVC Application with examples. Please read our previous Video before proceeding to this Video where we discussed ViewBag in the ASP.NET MVC application. At the end of this Video, you will understand What is Strongly Typed View in ASP.NET MVC and when and how to use strongly typed view in MVC Application.

In ASP.NET MVC, we can pass the data from the controller action method to a view in many different ways like ViewBag, ViewData, TempData and strongly typed model object. If we pass the data to a View using ViewBag, TempData, or ViewData, then that view becomes a loosely typed view. Here we will discuss how to create a strongly typed view in the ASP.NET MVC application.

Creating Strongly Typed View in MVC

In order to create a strongly typed view in ASP.NET MVC application, we need to pass the model object as a parameter to the View() extension method. The Controller base class provide us the following four overloaded versions of View() extension method which we can use to pass the model data from the controller action method to a view.

Strongly Typed View in ASP.NET MVC Application

We are going to use the overloaded version which takes only the model object as the input parameter. As the input parameter is of object type, so we can pass any data. Modify the Index action method of the Home Controller as shown below to pass the Employee object as a parameter to the View extension method.

using FirstMVCDemo.Models;
using System.Web.Mvc;
namespace FirstMVCDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer();
Employee employee = employeeBL.GetEmployeeDetails(101);
ViewBag.Header = "Employee Details";
return View(employee);
}
}
}
Changes in Index.cshtml View:

In order to create a strongly typed view in ASP.NET MVC Application, we need to specify the model type within the view by using the @model directive. As here, the Employee class is going to be the model so we need to specify the model directive as shown below.

@model FirstMVCDemo.Models.Employee

The above statement will tell that we are going to use FirstMVCDemo.Models.Employee as the model for this view. Here in the directive (@model), the letter is in lowercase and the statement should not be terminated with the semicolon.

Then we can access the model properties simply by using @Model, here the letter M is in uppercase. So, in our example, we can access the Employee object properties such as Name, Gender, City, Salary, etc. by using @Model.Name, @Model.Gender, @Model.City, and @Model.Salary respectively.

So Modify the Index.cshtml view file as shown below to make the view as strongly typed.
@model FirstMVCDemo.Models.Employee
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Page Title</title>
</head>
<body>
<h2>@ViewBag.Header</h2>
<table style="font-family:Arial">
<tr>
<td>Employee ID:</td>
<td>@Model.EmployeeId </td>
</tr>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender:</td>
<td>@Model.Gender</td>
</tr>
<tr>
<td>City:</td>
<td>@Model.City</td>
</tr>
<tr>
<td>Salary:</td>
<td>@Model.Salary</td>
</tr>
<tr>
<td>Address:</td>
<td>@Model.Address</td>
</tr>
</table>
</body>
</html>

That’s it. Now run the application and navigate to the “/Home/Index” URL and you will see the employee data as expected in the webpage.

Advantages of using Strongly Typed View in ASP.NET MVC Application: 

We will get the following advantages when we use strongly typed views in the ASP.NET MVC application.

  1. Strongly Typed View in ASP.NET MVC provides compile-time error checking as well as intelligence support.
  2. If we misspell the property name, then it comes to know at compile time rather than at runtime.

In our example, we are still using ViewBag to pass the Header from the Controller to the View. Then the question that should come to your mind is how we will pass the Header to a strongly typed view without using ViewBag. Well, we can do this by using the View Model in the ASP.NET MVC application.

See All

Comments (269 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /269

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /269

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /269