The model classes represents domain-specific data and business logic in the MVC application. It represents the shape of the data as public properties and business logic as methods. In the ASP.NET MVC Application, all the Model classes must be created in the Model folder.
Models in ASP.NET Core MVC Application
In this Video, I am going to discuss the Models in ASP.NET Core MVC application with Examples. Please read our previous Video where we discussed Application. We are going to work with the same example that we created in our Video.
What is a Model in ASP.NET Core MVC?
A model is a class with .cs (for C#) as an extension having both properties and methods. Models are used to set or get the data. If your application does not have data, then there is no need for a model. If your application has data, then you need a model.
What is the Role of Models in ASP.NET Core MVC?
The Models in ASP.NET Core MVC contains a set of classes that are used to represent the domain data (you can also say the business data) as well as it also contains logic to manage the domain/business data. So, in simple words, we can say that the models in ASP.NET Core MVC Application are used to manage the data i.e. the state of the application in memory.
If you are working with any Web Application that is based on MVC Design Pattern, then in that MVC Application, three things are common i.e. Model, View, and Controller. The Controllers are used to manage the overall flow of the application. Models are responsible for the data and these data are used on Views. Views are basically the HTML pages that get rendered into the browser of the client. In the browser, we generally perform two operations. First, we display the data to the user and secondly, we get the data from the user. And for both these operations models are used.
Adding Models Folder in ASP.NET Core Application:
Right-click on your project, then select add => new folder option from the context menu which will add a new folder. Then rename the folder name as Models. Here we want to create a model for displaying the student data. So, create a class file with the name Student.cs within the Models folder. Once you create the Student model then the folder structure of your application should looks as shown below.
Now open the Student.cs class file and then copy and paste the following code.
This is our student model which is going to store the student data in memory. As we already discussed, the model in ASP.NET Core MVC Application also contains business logic to manage the data. So, in our example, to manage the student data i.e. to perform the CRUD operation on the student data we are going to use the following IStudentRepository interface.
Creating IStudentRepository interface:
Right-click on the Models folder and then add an interface with the name IStudentRepository.cs. Once you create the interface then copy and paste the following code in it.
As you can see, we created the above interface with one method i.e. GetStudentById() method which will retrieve the student details by the student id.
Creating TestStudentRepository class:
Let us create an implementation class for the above IStudentRepository interface. In our upcoming Video, we will discuss how to retrieve the student details from a database. But for this demo, lets hardcoded the student details. So, create a class file with the name TestStudentRepository.cs within the Models folder and then copy and paste the following code in it.
Modify HomeController:
We already created a Controller with the name HomeController within the Controller Folders. If you have not created yet then add a class file with the HomeController within the Controllers folder. And then modify the HomeController as shown below to use the TestStudentRepository to retrieve the student details. The Student and TestStudentRepository are present in a separate namespace, so you need to include the namespaces as well.
If you are directly come to this Video, without reading our previous Video, then please modify the Startup class as shown below where we register the MVC service to the built-in dependency Injection Container as well as we add MVC Middleware to the application request processing pipeline.
Now run the application and navigate to http://localhost:
The way we implemented the GetStudentDetails method of Home Controller is not loosely coupled. That means tomorrow if the implementation class of the IStudentRepository is changed then we need to change the code in the Home Controller class as both are tightly coupled. We can overcome this problem by implementing a dependency injection design pattern.