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.
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.
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 m 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.
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.
- Strongly Typed View in ASP.NET MVC provides compile-time error checking as well as intelligence support.
- 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.