ASP.NET MVC - Models
To learn ASP.NET MVC, we will build an Internet application.
Part 7: Adding the Data Model.
MVC Model
The MVC model contains all application logic (business logic, validation logic, data access logic) except pure view and controller logic.
With MVC, the model can control and manipulate application data.
Models Folder
The Models folder contains classes that represent the application models.
Visual Web Developer automatically creates an AccountModels.cs file, which contains models for application security.
AccountModels includes LogOnModel, ChangePasswordModel, and RegisterModel.
Adding a Database Model
The database model required for this tutorial can be created with a few simple steps:
In the Solution Explorer window, right-click the Models folder, and select Add and Class.
Name the class MovieDB.cs and click Add.
Edit the class:
Note:
We intentionally named the model "MovieDB". In the previous chapter, you saw "MovieDBs" (ending with an 's') for the database table. This may seem a bit odd, but this naming convention ensures that the model connects to the database table, and you must use it this way.
Adding a Database Controller
The database controller required for this tutorial can be created with a few simple steps:
Rebuild your project: Select Debug, then select Build MvcDemo from the menu.
In Solution Explorer, right-click the Controllers folder, select Add and Controller.
Set the controller name to MoviesController.
Select the template: Controller with read/write actions and views, using Entity Framework
Select the model class: MovieDB (MvcDemo.Models)
Select the data context class: MovieDBContext (MvcDemo.Models)
Select the view: Razor (CSHTML)
Click Add
Visual Web Developer will create the following files:
MoviesController.cs file in the Controllers folder
Movies folder in the Views folder
Adding Database Views
In the Movies folder, the following files will be automatically created:
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
Congratulations
Congratulations. You have added your first MVC data model to the application.
Now you can click the "Movies" tab.