ASP.NET MVC Translated for the Web Forms Programmer (5 in a series) - What the Frig Is a View Model?
I wanted to expound on something very important that I kind of glazed over in the last post, View Models. View Models are something that is very pivotal to MVC and were one of the hardest things for me to understand. I feel like it's important to go over them in depth so you can understand them as well.
I actually don't love the concept of a View Model, it's a little muddy for me. I understand that it is important because it allows the View to do what it was made for: rendering and displaying elements. However, like any other programmer I like things clean and easily defineable. I like concrete answers, and non-redundant interfaces. A View Model is none of these things. It is an arbitrary collection of data gathered for the express purpose of conveiniently grouping your views.
Before I completely talk you out of them I must clarify that I love using them in my MVC applications because it alows me to seperate my OO archtecture from my Views. You know, seperation of concerns and all that.
So how do I get started? A View Model is simply a class that you build to aggregate the data you want to display in your view.
Let's say I have a class Car and a class Train. I want a view to display all the ways I can get from New Jersey to New York. I have a train schedule and a list of rental cars. The classes Train and Car are not associated to each other in my data model. So, what to do? That is where our View Model comes in. They are pretty easy to create, my Car/Train view Model is called TransportationViewModel and it looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Transportation.Models;
namespace Transportation.ViewModels
{
public class TransportationViewModel
{
public List<Car> Cars {get; set;}
public Train Train {get; set;}
}
}
To populate my View in my Controller I create an action and when I return my View I pass in an instance of my TransportationViewModel. Like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Transportation.ViewModels;
using Transportation.Models;
namespace Transportation.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult ListVehicles()
{
List<Car> cars = new List<Car>
{new Car{Make = "Honda", Model="Accord"},
new Car{Make = "Acura", Model="Integra"}};
Train train = new Train { Line = "NorthEast Corridor", Schedule = "Monday at 8" };
TransportationViewModel t = new TransportationViewModel
{
Cars = cars,
Train = train
};
return View(t);
}
}
}
As you can see this is just a simple class that contains both a list of Cars and a Train. My view will be strongly typed and inherit from this View Model. This is how it will look:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Transportation.ViewModels.TransportationViewModel>" %>
<%@ Import Namespace="Transportation.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ListVehicles
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>List Vehicles</h2>
<h4>Cars:</h4>
<% foreach(Car c in Model.Cars)
{%>
Make: <b><%=c.Make %></b>
Model: <b><%=c.Model %></b>
<% } %>
<h4>Train:</h4>
<b><%=Model.Train.Line %></b>
<b><%=Model.Train.Schedule %></b>
</asp:Content>
As you can see, my View Model allows me to pass my Data to my view and I don't have to muss with my objects or my model.
When it comes to data manipulation you want to keep that in the Model, not do it in your View Model. Unless what you are messing with is specifically for that particular view.
Now, the question of the year is: "Do I need a new View Model for each View?" The answer is: it's up to you. If you only need one class in your particular view than you don't need one. However, I find that to be very rare.
Hi nerds, I’m Sara and I’m a girl developer, or a “gerd” if you will. I am an ASP.NET/C#/SQL software engineer. I like to think I have lots of hats though. I’ve worked on all different sized projects, high volume and low volume sites. Different types of databases. One thing that is very unique about the project I am on right now is that I’m doing it all by myself! I am used to working with a team of awesome developers. Sara is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website.
- Login or register to post comments
- 756 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)









