Posted on 3/28/2020 12:25:19 PM by Admin

Model & strongly Typed View

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

In layman term 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 bind with any model is called as strongly typed view. We can bind any class as model to view and can access model properties on that view. We can also use data associated with model to render controls .

Strongly-typed view has 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 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 model object from 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 employeeList = new List()
            {
                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