ASP.NET MVC - Model
To learn ASP.NET MVC, we will build an Internet application.
Part 7: Add a data model.
The MVC model
MVC Modelcontains all application logic other than pure view and controller logic (business logic, validation logic, data access logic).
With MVC, the model can control and manipulate application data.
The Models folder
The Models folderContains the classes that represent the application model.
Visual Web Developer automatically creates a AccountModels.cs file that contains the models used for application security.
AccountModels Contains LogOnModel、ChangePasswordModel and RegisterModel。
Add a database model
The database model required by this tutorial can be created in a few simple steps:
- in Solution Explorerin the window, right-click the **Models** folder and select Add and Class。
- Name the class MovieDB.cs, then click Add。
- to edit this class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcDemo.Models
{
public class MovieDB
{
public int ID { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public DateTime Date { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet
}
}
Note:
We deliberately named the model "MovieDB". In the previous chapter, you saw "MovieDBs" (ending with s) used for the database table. This looks a bit odd, but this naming convention ensures the model connects to the database table, and you must use it this way.
Add a database controller
The database controller required by this tutorial can be created in a few simple steps:
- Rebuild your project: select Debug, then select **Build MvcDemo** from the menu.
- In the Solution Explorer, right-click 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 views Razor (CSHTML)
- Click Add
Visual Web Developer will create the following files:
- Controllers the MoviesController.cs file
- Views the Movies folder in the window
Add a database view
In the Movies folder, the following files will be created automatically:
- 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.