WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET MVC - Models


To learn ASP.NET MVC, we are Building an Internet Application.

Part VII: Adding a Data Model.


MVC Models

The MVC Model contains all application rationale (business rationale, approval rationale, and information get to rationale), aside from unadulterated view and controller logic.

With MVC, models both hold and control application data.


The Models Folder

The Models Folder contains the classes that speak to the application display.

Visual Web Developer consequently makes a AccountModels.cs record that contains the models for application security.

AccountModels contains a LogOnModel, a ChangePasswordModel, and a RegisterModel.


Adding a Database Model

The database demonstrate required for this instructional exercise can be made with these straightforward steps:

  • In the Solution Explorer, right-click the Models organizer, and select Add and Class.
  • Name the class MovieDB.cs, and click Add.
  • Edit the class:
using System;
using System.Collections.Generic;
utilizing 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 have purposely named the model class "MovieDB". In the past part, you saw the name "MovieDBs" (finishing with s) utilized for the database table. It looks odd, yet this is the naming tradition you need to use to influence the model to interface with the database table.


Adding a Database Controller

The database controller required for this instructional exercise can be made with these basic steps:

  • Re-Build your venture: Select Debug, and after that Fabricate MvcDemo from the menu.
  • In the Solution Explorer, right-click the Controllers envelope, and select Add and Controller
  • Set controller name to MoviesController
  • Select format: Controller with read/compose activities and perspectives, utilizing Entity Framework
  • Select show class: MovieDB (MvcDemo.Models)
  • Select information setting class: MovieDBContext (MvcDemo.Models)
  • Select sees Razor (CSHTML)
  • Click Add

Visual Web Developer will make the accompanying files:

  • A MoviesController.cs record in the Controllers folder
  • A Movies envelope in the Views folder

Adding Database Views

The following records are consequently made in the Movies folder:

  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml
  • Index.cshtml

Adding a Connection String

Add the accompanying component to the <connectionStrings> component in your Web.config file:

<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|\Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>

Congratulations

Congratulations. You have included your first MVC information model to your application.

Now you can tap on the "Movies" tab :- )