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
Select Code-> Class and give some name – I have given Movie
And the following code will be generated
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.
- We create instance of movie collection class
- Seed the collection with some movies
- Get the list back
- 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.
- 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.
- 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.
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.