Strongly Typed View Vs Dynamically Typed View In ASP.NET MVC

In this article, we will find and learn the difference between Strongly Typed View vs Dynamically Typed View in ASP.NET MVC. In the previous ASP.NET MVC tutorials, we saw:
Let's Begin: (Dynamic Typed View)

Create a new ASP.NET Web Application.


Select Empty MVC Template and click on OK.


Right click on Models folder and Add Employee Entity class in Model.


Employee.cs Code
  1. public class Employee  
  2. {  
  3.     public int ID  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Name   
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string Designation  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
Right Click on controller and Add New empty controller and give it a name.


Create an object of Employee model and initialize it property with some data and pass the employee object to the view.


Now add a view for Index Action method of DynamicController. Select Empty Template(without model) as we are going to create Dynamic Typed View.


By default, the model property available within the views is dynamic, which means that we can access the value of Model without knowing its exact type. We can also create a dynamic-typed view by declaring @model dynamic (page as dynamic typed). As we are using dynamic view, Artificial Intelligence doesn't help us and dynamic expression resolved at runtime. If we type or use property that is not present/related to model then an exception is thrown during runtime. 


Build and Run the application.


Strongly Typed View

Let's add another controller and name it as StronglyTypedController and add the same code in Index action method of StronglyTypedController.

  1. public class StronglyTypedController : Controller    
  2. {    
  3.         public ActionResult Index()    
  4.         {    
  5.             Employee employee = new Employee()    
  6.             {    
  7.                 ID = 101,    
  8.                 Name = "Anoop Kumar Sharma",    
  9.                 Designation = "Software Engineer"    
  10.             };    
  11.             //Passing the Employee obj to View    
  12.             return View(employee);    
  13.         }    
  14. }    
This time we will select Empty Template. In above example we have selected Empty Template without model class.


Select Employee Model class from the drop-down in order to create a strongly-typed view. Make sure to compile the project before selecting a model class from the dropdown because the list i.e. Model class list in add view dialog populated with the help of reflection.


Add View for Index action method of StronglyTypedController.


@model DynamicVsStronglyTypedView.Models.Employee specifies the datatype for the model. @Model and @model look similar but they are different in reality. @Model is a property of the view that refers to the model that was passed to the view from the controller. Inside strongly-typed view, we get Intelligence and compile time error checking support.

Build and Run the application.



 I hope you like it. Thanks!
Read more articles on ASP.NET:

Up Next
    Ebook Download
    View all
    View all