Models in ASP.NET MVC Explained
A model is a class
that contains the business logic of your application. It also used for
accessing data from the database. The model class does not handle directly
input from the browser. In MVC, it is the Controller that handle input from the
browser directly and process the request by receiving data from the model and
pass it back to view as response. It does not contain any HTML code either.
It is a best practice but not mandatory for
developers to not have any communications with the view directly, models should
only contain a POCO (Plain Old CLR Objects) classes. All processing
logic and communication with the view should be handled by another layer
called Viewmodels.
Note that it is
not mandatory, but it is a good programming practice to store all model classes
within the Models folder of MVC application.
Creating a Model
Let's add a new model in exiting project created in our previous
article here.
Model contains set and get for its properties.
To add the model, just right click on the Models folder
of the project, select Add then click on New Item as shown below
Under Visual C# installed template, click
on Code on the left hand side of the Add new Item window select Class from the
middle panel of the window as shown below;
Enter the name of the model class as
Employee and click on Add as shown below;
Enter the properties of our Employee Class (Model) as follows;
Id, First Name, Middle Name, Last Name, Sex, Date of Birth,
Address and phone number
The properties should entered in between the opening and closing
curly brace of the Employee Class as shown below
This article is referenced from
the JavaTPoint here
Comments
Post a Comment