Hello friends, I hope you all are doing great. Today, I am going to share the second tutorial in ASP.NET MVC series and I will explain the concept of Model in ASP.NET MVC. In our previous tutorial, we have seen How to Setup Your First Project in ASP.NET MVC.
So, before developing our web app, we first have to discuss some concepts in MVC Framework and Model is one of them, which we will cover in today's tutorial. So, let's get started with it:
What is a Model in ASP.NET MVC ?
In, ASP.NET MVC Framework structure, we have to understand 3 very simple blocks, which are:
Model connects our web application with our SQL Database, it takes commands from the Controller and then get or set values in the Database and finally updates the View.
We can say that Model is acting as a middle man between Controller, View & SQL Databases.
If you are not understanding the Controller or View, that's no problem. We will cover them in next chapters, for now just focus on functioning of Model.
So, in your Solution Explorer, you can see a folder named Models, click to expand and you will find 3 model files in it, as shown in below figure:
You can see in above figure that Microsoft visual studio has auto created 3 Model files in our ASP web project.
So, click AccountViewModels.cs file and it will open up as shown in below figure:
Now you can see in above figure that our model file has some classes in it.
If you check the whole file then you will find more than 5 classes in it.
Each of these class is actually representing a unique model, all of these classes have certain rules attached to them.
Now let's understand the working of our web app.
When our ASP web application runs for the first time:
The App goes to Controller.
Controller hits the Model.
Model gets or sets values into SQL Database & then return value to Controller.
Controller returns value to View.
I have shown this working in block diagram form here:
Now in this model file, you have find for class RegisterViewModel, its the Model for our registration page, shown in below figure:
The main thing to note in this class is that, we have created 3 variables with data types public string, named as:
The parameters assigned to these variables are get and set, and these variables will either Get value from the SQL Database or Set new value to it.
So in simple words, the model is dealing with data in or out, it receives commands/data from controller and according to the rules set in each model, the data will be either stored in the SQL database or fetched from it.
Model tells us, what kind of information is required and what kind of information is placed in the SQL Databases.
The major code of MVC Framework is written in the Model section, because here we have to write all our rules and permissions to interact with the SQL databases.
Here's a video where one of our team member has explained Model in ASP.NET MVC in detail:
So, that was all about today's tutorial on Model in ASP.NET MVC. I hope you have got the idea. In the next tutorial, we will have a look at What is View in ASP.NET MVC. Take care !!!
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow