Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET MVC Model

ASP.NET MVC - Model


To learn ASP.NET MVC, we will build an Internet application.

Part 7: Add a data model.


The MVC model

MVC Modelcontains all application logic other than pure view and controller logic (business logic, validation logic, data access logic).

With MVC, the model can control and manipulate application data.


The Models folder

The Models folderContains the classes that represent the application model.

Visual Web Developer automatically creates a AccountModels.cs file that contains the models used for application security.

AccountModels Contains LogOnModel、ChangePasswordModel and RegisterModel。


Add a database model

The database model required by this tutorial can be created in a few simple steps:

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 Movies { get; set; }

}

}

Note:

We deliberately named the model "MovieDB". In the previous chapter, you saw "MovieDBs" (ending with s) used for the database table. This looks a bit odd, but this naming convention ensures the model connects to the database table, and you must use it this way.


Add a database controller

The database controller required by this tutorial can be created in a few simple steps:

Visual Web Developer will create the following files:


Add a database view

In the Movies folder, the following files will be created automatically:


Congratulations

Congratulations. You have added your first MVC data model to the application.

Now you can click the "Movies" tab.