How to create a simple website in ASP.NET MVC
Part four of a seven-part series of blogs

This blog explains the steps you'll need to follow to create a simple one page website in ASP.NET MVC. The aim of the blog is not to act as a tutorial, but to help people choose between ASP.NET MVC and classic ASP.NET.

  1. An overview of creating a website in ASP.NET MVC
  2. Creating an ASP.NET MVC website
  3. Creating an entity model in MVC
  4. Creating a view model (this blog)
  5. Ensuring MVC shows the right page - router and controller
  6. Creating our view
  7. Conclusion

Posted by Andy Brown on 16 June 2014

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Creating a view model

Every webpage that you create in ASP.NET MVC has to be based on a view model, which is a class giving all of the things that the page might want to use.

There are at least two things wrong with this statement: MVC uses views, not pages, and they don't actually have to be based on view models, but for the purposes of this blog it's reasonable to gloss over these points.

Creating the view model to start with

It's a good idea (I think) to put your view models in a separate folder:

View model folder

Create a new folder (here called ViewModels), and right-click on it to add a new view model.

View Model doesn't appear in the list, because all that you're actually doing is creating a class:

Creating a new class

Choose to create a class (that's what a view model is).

Here I've called my new view model vmFilm:

The new view model

Give your new view model a suitable name, as here.


Referencing the entity model

You can now create a reference to the namespace containing the entity model (or in English, make sure that MVC knows where to find your database):

// allows us to use the model we've just created

using MvcApplication4.Models;

// these are added automatically by Visual Studio

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

The rest of the class will use LINQ to get at the table of films in your database:

namespace MvcApplication4.ViewModels


public class vmFilm


// create a "data context" for this controller

protected MoviesEntities movies = new MoviesEntities();

public List Films


get {

// return a set of films

return (

from f in movies.tblFilm

select f






The truly amazing thing about this is that Visual Studio intellisense will let you complete the LINQ command:

The data model completing

No need to create any additional classes or properties - MVC knows all about the table of films and the columns within it from your entity model.

To write a good MVC system you're going to need to learn the LINQ language well.  It is functionally similar to SQL, but written in a different syntax.

Now we've got the view model, it's time to set up the router and controller for our website.

This blog has 0 threads Add post