Understand ASP. net mvc Programming Model Chapter 1 data model, asp. netmvc
MVC model
MVCModelContains all application logic (business logic, verification logic, data access logic), except for pure view and controller logic. Through MVC, the model can save and operate application data.
Models folder
Models folderContains classes that represent the application model.
Take logon verification as an example to createAccountModels. csFile for the application security model.
AccountModelsIncludeLogOnModel,ChangePasswordModelAndRegisterModel.
LogOnModel:
public class LogOnModel{[Required][Display(Name = "User name")]public string UserName { get; set; }[Required][DataType(DataType.Password)][Display(Name = "Password")]public string Password { get; set; }[Display(Name = "Remember me?")]public bool RememberMe { get; set; }}
RegisterModel:
public class RegisterModel{[Required][Display(Name = "User name")]public string UserName { get; set; }[Required][DataType(DataType.EmailAddress)][Display(Name = "Email address")]public string Email { get; set; }[Required][StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)][DataType(DataType.Password)][Display(Name = "Password")]public string Password { get; set; }[DataType(DataType.Password)][Display(Name = "Confirm password")][Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]public string ConfirmPassword { get; set; }}
ChangePasswordModel:
public class ChangePasswordModel{[Required][DataType(DataType.Password)][Display(Name = "Current password")]public string OldPassword { get; set; }[Required][StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)][DataType(DataType.Password)][Display(Name = "New password")]public string NewPassword { get; set; }[DataType(DataType.Password)][Display(Name = "Confirm new password")][Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]public string ConfirmPassword { get; set; }}
Add database model
InSolution Manager, Right-clickModelsFolder, selectAdd,Class.
To addMovieDB. csExample,
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<MovieDB> Movies { get; set; } }}
Add a database Controller
Visual Studio creates the following files:
- MoviesController. cs file in the Controllers folder
- Movies folder in Views folder
Add database view
The following files are automatically created in the Movies Folder:
- Create. cshtml
- Delete. cshtml
- Details. cshtml
- Edit. cshtml
- Index. cshtml