MVC model
The MVC model contains all the application logic (business logic, validation logic, data access logic), in addition to pure view and controller logic. With MVC, models can save and manipulate application data.
Models folder
The Models folder contains classes that represent the application model.
Take login verification as an example to create a AccountModels.cs file for the application security model.
accountmodels contains logonmodel,Changepasswordmodel , and Registermodel.
Logonmodel:
Public classLogonmodel{[required][display (Name="User name")] Public stringUserName {Get;Set; } [Required] [DataType (Datatype.password)] [Display (Name="Password")] Public stringPassword {Get;Set; } [Display (Name="Remember me?")] Public BOOLRememberMe {Get;Set; }}
Registermodel:
Public classRegistermodel{[required][display (Name="User name")] Public stringUserName {Get;Set; } [Required] [DataType (Datatype.emailaddress)] [Display (Name="Email Address")] Public stringEmail {Get;Set; } [Required] [Stringlength ( -, errormessage ="The {0} must is at least {2} characters long.", Minimumlength=6)][datatype (Datatype.password)][display (Name="Password")] Public stringPassword {Get;Set; } [DataType (Datatype.password)] [Display (Name="Confirm Password")][compare ("Password", errormessage ="The password and confirmation password DoNot match.")] Public stringConfirmPassword {Get;Set; }}
Changepasswordmodel:
Public classChangepasswordmodel{[required][datatype (Datatype.password)][display (Name="Current Password")] Public stringOldPassword {Get;Set; } [Required] [Stringlength ( -, errormessage ="The {0} must is at least {2} characters long.", Minimumlength=6)][datatype (Datatype.password)][display (Name="New Password")] Public stringNewPassword {Get;Set; } [DataType (Datatype.password)] [Display (Name="Confirm New Password")][compare ("NewPassword", errormessage ="The new password and confirmation password DoNot match.")] Public stringConfirmPassword {Get;Set; }}
Add a Database model
In solution Manager , right-click the Models folder and select Add , class .
To add MovieDB.cs as an example ,
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Data.Entity;namespacemvcdemo.models{ Public classmoviedb{ Public intID {Get;Set; } Public stringTitle {Get;Set; } Public stringDirector {Get;Set; } PublicDateTime Date {Get;Set; }} Public classmoviedbcontext:dbcontext{ PublicDbset<moviedb> Movies {Get;Set; } }}
Adding a Database controller
- In Solution Explorer, right-click the Controllers folder and select Add, Controller.
- Set the controller name to Moviescontroller
- Select Template: Controller with read/write actions and views, using Entity Framework
- Select Model class: Moviedb (Mvcdemo.models)
- Select data Context class: Moviedbcontext (Mvcdemo.models)
- Select View Razor (CSHTML)
- Click Add
Visual Studio creates the following files:
- MoviesController.cs files in the Controllers folder
- The Movies folder in the Views folder
To add a database view
The following files are automatically created in the Movies folder:
- Create.cshtml
- Delete.cshtml
- Details.cshtml
- Edit.cshtml
- Index.cshtml
Understanding the MVC programming model in ASP. 3rd Data Model