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