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