MVC model
ASP.NET MVC - Model
To learn ASP.NET MVC, we will build an Internet application.
Part 7: Adding the data model.
MVC Model
MVC Model Contains all other application logic (business logic, validation logic, data access) except pure view and controller logic logic).
Through MVC, models can control and manipulate application data.
Models Folder
Models Folder Contains classes that represent application models.
Visual Web Developer automatically creates an AccountModels.cs file that contains the models used for application security.
AccountModels contains LogOnModel, ChangePasswordModel, and RegisterModel.
Add database model
The database model required for this tutorial can be created by following a few simple steps:
InSolution ExplorerIn the window, right-click Models folder and select Add and Class.
Name the class MovieDB.cs and click Add.
Edit this class:
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 deliberately named the model "MovieDB". In the previous chapter, you've seen "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.
Add Database Controller
The database controller required for this tutorial can be created in a few simple steps:
Rebuild your project: Select Debug, then from the menu BuildMvcDemo.
In Solution Explorer, right-click the Controllers folder and select Add and Controller.
Set the controller name to MoviesController.
Select 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 ViewRazor (CSHTML)
Click Add
Visual Web Developer will create the following files:
Controllers in the folder MoviesController.cs File
Views Folder Movies Folder
Add database view
In the Movies folder, the following files will be automatically created:
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
#Congratulations
Congratulations. You've added your first MVC data model to your application.
Now you can click on the "Movies" tab.