Model in ASP.Net MVC

Sidenote : This is the part 5 of the ASP.Net MVC course series.  You can get all the parts of this tutorial here.

In the past chapter, we have used local objects in View to show the information to the users. We should not  use any data/business rules in View as it violates the Separation of concerns principle. Views should do one and only thing – display information to the users.

Model layer contains the business rules and is responsible for getting the required data. Model will give the data to the controller and controller will pass the model data to the view so that View can display that information to the users.

We are going to use the model to have the data and we’ll use this model data for showing the movie list in the view.

Open our MicroIMDBV1 solution.

Right Click Models Folder – > Select Add-> Select New Item

Model - Add

Select Code-> Class and give some name – I have given Movie

Add New Item - Model Class

And the following code will be generated

Movie cs file

We have created model classes Movie to represent movie and Movies to hold collection of movies. Though in real world, you would not create movie collection class as it would be stored in database. To illustrate the role of Model layer with simple example, I have done so.

The Index action method in movie controller is updated with the following changes – so that view can make use of the model class that we have defined.

  1. We create instance of movie collection class
  2. Seed the collection with some movies
  3. Get the list back
  4. Pass the model(list of movies) to the view

Sidenote: There are few other ways to pass the data from Controller to View such as ViewData,ViewBag or TempData which would be discussed later.

In the View(Index.cshtml), you just need to make the below changes to use the model in your View.

  1. Include the model at the first line of the view. It can be included with keyword @model followed by model type. Since it’s a list(of movies), we have made it as IEnumerable.
  2. You can use the model in the view by accessing Model variable(please note the capital M in the model here)

When you run the application now, you’ll get the following result.

Index Result

Summary:

In the beginning, we have built MVC application only with Controller – directly returning text from the controller to the user. Also we saw how we may run into the issues if we have to send html file with multiple html elements, if we follow this way. And we came to know the purpose of View.

And we delegated the task of building the UI to the View. Now, View took care of the representation of UI. In real world of huge web application, there may be few UI developers who would be working on the front end. So these UI developers can give you the View code which you can modify according your data needs. So this ‘separation of concerns’ made things moving in parallel.

We have created the data objects in View and displayed it. But View should have one and only purpose- to display data. Later, we have even moved this data objects to model so that model can take care of business logic and data access.

In practice, Model layer will have just domain classes – particular to your business domain of yourself and will not contain data objects.  The model layer will not even know what type of data access method(Entity Framework,ADO.Net) we are going to use. Data access layer will use this domain classes to create objects and persist them with the help of Entity framework.

[button type=”flat” shape=”square” size=”large” href=”https://www.dotnetodyssey.com/asp-net-mvc-5-free-course/layout/” title=”Previous chapter”]Previous Chapter[/button] [button type=”flat” shape=”square” size=”large” href=”https://www.dotnetodyssey.com/asp-net-mvc-5-free-course/” title=”Home”]Home[/button] [button type=”flat” shape=”square” size=”large” href=”https://www.dotnetodyssey.com/asp-net-mvc-5-free-course/entity-framework/” title=”Next Chapter”]Next Chapter[/button]