Create a New Model in ASP.NET MVC
[vc_row full_width_row="true"][vc_column][vc_column_text]
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at How to
Create a New Model in ASP.NET MVC. It's 5th tutorial in ASP MVC series. We have already seen the basic concept of Model in ASP.NET MVC in our second tutorial. Models are used to communicate data from SQL Database to respective Controllers, which is then displayed by the Views to the user.
When you are designing some professional website then it happens a lot that you need to place different data in your databases, normally this data is coming from users. So, in such cases you have to create new models and then assign respective controllers to them. So, today we will see How to do the first part i.e.
creating new models in asp.net mvc.
Create a New Model in ASP.NET MVC
- Models in ASP.NET MVC are simple C# classes and if you look at their file extension then it's .cs, so we just need to create a new C# class file.
- So, in your Solution Explorer, right click on the Models folder.
- Now click on the Add and then click on Class, as shown in below figure:
- When you click on the Class, it will open up a new window.
- In this new window, you need to select Class and then write some name for it, as I have given it a name StudentModel.cs.
- Finally, click the Add Button, these settings are shown in below figure:
- When you click on this Add Button, visual studio will automatically create a new Model file in your Models Folder.
- This new Model File will also open up in your workspace and will have small default code in it, as shown in below figure:
- Now I will create some attributes in my new Model File, where I ask for some basic registering details from my students.
- I will use them later after creating a new Controller & View for this model, but rite now we are just doing the server side programming, there's no client side involved.
- So, let's have a look at the Model Code:
- You can see in above figure that I have created four variables inside my new StudentModel class.
- All these variables have a datatype of string and are initialized with two further attributes get & set.
- When we need to get value from SQL, then we use get & when we want to save some value then we use set.
Note:
- I am not sharing any codes because we are in initial phases & it's more about understanding the flow of coding.
- I will start share the code once we start writing it, rite now there's not much coding part.
- Let's have a look at the video explanation for better understanding:
So, that was all about Creating new Model in ASP.NET MVC. In the coming lecture, we will create new Controller in ASP.NET MVC for this model. So, take care & have fun !!! :)
[/vc_column_text][/vc_column][/vc_row]