ASP.NET MVC Models

## ASP.NET Tutorial

ASP.NET HOME ASP.NET Intro

## WP Tutorial

WebPages Intro WebPages Razor WebPages Layout WebPages Folders WebPages Global WebPages Forms WebPages Objects WebPages Files WebPages Databases WebPages Helpers WebPages WebGrid WebPages Charts WebPages Email WebPages PHP WebPages Publish WebPages Examples

## WP References

WebPages Classes WebPages Security WebPages Database WebPages WebMail WebPages Helpers

## ASP.NET Razor

Razor Intro Razor Syntax Razor C# Variables Razor C# Loops Razor C# Logic Razor VB Variables Razor VB Loops Razor VB Logic

## ASP.NET MVC

MVC Intro MVC Application MVC Folders MVC Layout MVC Controllers MVC Views MVC Database MVC Models MVC Security MVC HTML Helpers MVC Publish MVC Reference

## WF Tutorials

WebForms Intro WebForms Pages WebForms Controls WebForms Events WebForms Forms WebForms ViewState WebForms TextBox WebForms Button WebForms Data Binding WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML Files WebForms Repeater WebForms DataList WebForms DbConnection WebForms Master Pages WebForms Navigation WebForms Examples

## WF References

WebForms HTML WebForms Controls WebForms Validation

# ASP.NET MVC - Models

« Previous
Next Chapter »

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 logic (business logic, validation logic, and data access logic), except pure view and controller logic.

With MVC, models both hold and manipulate application data.

## The Models Folder

The Models Folder contains the classes that represent the application model.

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

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

## Adding a Database Model

The database model needed for this tutorial can be created with these simple steps:

- In the Solution Explorer, right-click the Models folder, and select Add and Class.
- Name the class MovieDB.cs, and click Add.
- Edit the 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 have deliberately named the model class "MovieDB". In the previous chapter, you saw the name "MovieDBs" (ending with s) used for the database table. It looks strange, but this is the naming convention you have to use to make the model connect to the database table.

## Adding a Database Controller

The database controller needed for this tutorial can be created with these simple steps:

- Re-Build your project: Select Debug, and then Build MvcDemo from the menu.
- In the Solution Explorer, right-click the Controllers folder, and select Add and Controller
- Set controller name to MoviesController
- Select template: Controller with read/write actions and views, using Entity Framework
- Select model class: MovieDB (MvcDemo.Models)
- Select data context class: MovieDBContext (MvcDemo.Models)
- Select views Razor (CSHTML)
- Click Add

Visual Web Developer will create the following files:

- A MoviesController.cs file in the Controllers folder
- A Movies folder in the Views folder

## Adding Database Views

The following files are automatically created in the Movies folder:

- 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 :-)

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE