Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Monday, 4 April 2016

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:
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:
public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Designation { get; set; }
}
Right Click on controller and Add New empty controller and give it a name.


Create an object of Employee model and initialize its 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.
Preview:

Strongly Typed View:
Let's add another controller and named it as StronglyTypedController and add the same code in Index action method of StronglyTypedController.

public class StronglyTypedController : Controller
{
        public ActionResult Index()
        {
            Employee employee = new Employee()
            {
                ID = 101,
                Name = "Anoop Kumar Sharma",
                Designation = "Software Engineer"
            };
            //Passing the Employee obj to View
            return View(employee);
        }
}
This time we will select Empty Template. In above example we have seleteted 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.
Preview:
Hope you like it. Thanks.
[Download Source Code via Google Drive]

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook