Model & strongly Typed View

by Sachin Singh


Posted on Saturday, 28 March 2020

Tags: Model and Strongly-Typed View in MVC What is strongly typed view strongly typed view in mvc MVC Strongly Typed Views View in MVC "model model in mvc model in asp.net mvc model in C# mvc what is model in mvc mvc view view in mvc view in asp.net mvc view in C# mvc what is view in mvc

A model is simply a business entity in your application that may have some business rules defined.

In layman's terms a model is a class with fields, properties, and methods. Generally, a model only represents an Entity of your application like Employee, Customer, etc with properties or getters and setters.

The view which binds with any model has named a strongly typed view. We can bind any class as a model to look at and may access model properties thereon view. We can also use data associated with a model to render controls .

Strongly-typed view has the following advantages.
  1) Intellisense
  2) Compile time type checking
  3) Automatic scaffolding

In the previous article we have used ViewBag, ViewData, and TempData to pass information from the controller to view, remember all of these do boxing -unboxing and are error-prone due to no IntelliSense support, as they accept key as string.

So, it is always recommended to pass a model object from the controller to view and make your view strongly typed.

Let's understand it with an example.

Step 1.Create a class in your model folder with some properties like below.


  public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

Step 2.Create one Action Method to return view , populate the Employee object and pass it to the view like below.


         public ActionResult Index()
        {
            Employee emp = new Employee();
            emp.Id = 1;
            emp.Name = "Michael";
            emp.Age = 22;
           
            return View(emp);
        }

Step 3. Use the Employee class as model in your view in order to make your view strongly typed with Employee object.


  @using MVCExample.Models
  @model Employee
  @{
    ViewBag.Title = "Index";
    }
  <h2> Index</h2>
  @Model.Id
  @Model.Name
  @Model.Age

Please note that first I have imported the models namespace and then use Employee Type(class) as model for the view.

To access the properties of Employee object I have used the Model property of WebViewPage class, which is the parent class for every view in MVC.

Let's take an example with of List of Employee object.


     public ActionResult Index()
        {
            List<employee> employeeList = new List<employee>()
            {
                new Employee(){Id=1,Name="Ram",Age=24},
                 new Employee(){Id=2,Name="Shyam",Age=25},
                  new Employee(){Id=3,Name="Geetha",Age=26},
                   new Employee(){Id=4,Name="Raman",Age=27},
                    new Employee(){Id=5,Name="Newton",Age=70},
            };
            return View(employeeList);
        }


 
   @using MVCExample.Models
  @model IEnumerable<Employee>
   @{
    ViewBag.Title = "Index";
    }

  <h2> Index</h2>

  @foreach(var item in Model){
    
    <ul>
        <li>@item.Id</li>
        <li>@item.Name</li>
        <li>@item.Age</li>
    </ul>
   }

since I have passed a list of Employee from controller to view ,I had to use IEnummerable of Employee object as model so that I could iterate through the list.

Strongly Typed with List of Employee
Strongly Typed with List of Employee