Recently, I noticed a nice pattern in the Nerd Dinner application. Nerd Dinner uses strongly typed view model classes to pass data from a controller to a view. This pattern provides you with a convenient way of representing complex view data.

Note: If you haven’t downloaded the Nerd Dinner sample ASP.NET MVC application yet, you really should. It is a really easy to understand sample application that you can use as a great introduction to ASP.NET MVC. You can download it from the http://www.ASP.net/mvc page.

In an ASP.NET MVC application, you pass data from a controller to a view by using view data. There are two ways that you can use view data. First, you can use view data as an untyped dictionary. For example, the controller in Listing 1 returns a collection of products in its Index() action. And, the view in Listing 2 displays the collection by iterating through each item in the collection.

Listing 1 – Controllers\ProductController.cs

using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            // Create list of products
            var products = new List<Product>();
            products.Add(new Product("Laptop computer", 344.78m));
            products.Add(new Product("Bubble gum", 2.00m));
            products.Add(new Product("Toothpaste", 6.99m));

            // Add products to view data
            ViewData["products"] = products;

            // Return view
            return View();
        }

    }
}

Listing 2 – Views\Product\Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="MvcApplication1.Models" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    <% foreach (var item in (List<Product>)ViewData["products"]) 
       { %>
    
    
        <li> <%= item.Name %> </li>
    
    
    <% } %>

</asp:Content>

The view in Listing 2 is an untyped view. Notice that the products item from view data must be cast to a collection of product items before you can do anything with it.

Instead of using an untyped view, you can use a strongly-typed view. The controller in Listing 3 also returns a collection of products. However, in this case, the collection is assigned to the ViewData.Model property.

Listing 3 – Controllers\Product2Controller.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class Product2Controller : Controller
    {
        //
        // GET: /Product2/

        public ActionResult Index()
        {
            // Create list of products
            var products = new List<Product>();
            products.Add(new Product("Laptop computer", 344.78m));
            products.Add(new Product("Bubble gum", 2.00m));
            products.Add(new Product("Toothpaste", 6.99m));

            // Add products to view data
            ViewData.Model = products;

            // Return view
            return View();
        }

    }
}

Listing 4 – Views\Product2\Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Product>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    <% foreach (var item in Model) 
       { %>
    
    
        <li> <%= item.Name %> </li>
    
    
    <% } %>

</asp:Content>

In Listing 3, the products are assigned to the ViewData.Model property. As an alternative to explicitly assigning data to the ViewData.Model property, you can pass the data to the View() method. In other words, the following two code blocks are equivalent:

// Assign products to Model 

ViewData.Model = products; 
return View(); 

// Assign products to Model 

return View(products); 

The view in Listing 4 is a typed view. In Listing 4, the Model property represents the collection of products as a collection of products. In other words, you don’t need to cast the view data to a collection of product items before you use the Model property.

Notice the Inherits attribute in the <%@ Page %> directive. The Inherits attribute is used to cast the Model property to a strongly-typed collection of product items. It looks like this:

<%@ Page Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Product>>" %>

Kind of a mouthful, but it is clear what it means.

The easiest way to add a typed view into an ASP.NET MVC application is to right-click a controller action and select the menu option Add View. You can specify the type of the view in the Add View dialog (see Figure 1).

Figure 1 – Specifying the type of a view

clip_image002

Notice that, in Figure 1, I specified the view content as a List. Selecting this option results in a strongly-typed view that represents a collection of products. If I had picked Empty instead of List, then I would get a strongly-typed view that represents a single product.

In general, I prefer using typed views instead of untyped views. To my eyes, performing casts within a view looks ugly.

The view data class exposes only one Model property. So what do you do when you need to pass multiple data items from a controller to a view? For example, how do you pass both a collection of products and collection of product categories to a view using a typed view? The answer is to use a view model.

The controller in Listing 5 contains a class named ProductViewModel. This class exposes a property for both the collection of products and the collection of categories. And, the strongly-typed view in Listing 6 displays both the products and product categories.

Listing 5 – Controllers\Product3Controller.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    // View Model Classes

    public class ProductViewModel
    {
        public ProductViewModel(List<Product> products, List<Category> categories)
        {
            this.Products = products;
            this.Categories = categories;
        }

        public List<Product> Products { get; private set; }
        public List<Category> Categories { get; private set; }

    }
    
    
    // Controller Class

    public class Product3Controller : Controller
    {
       
        //
        // GET: /Product3/

        public ActionResult Index()
        {
            // Create list of products
            var products = new List<Product>();
            products.Add(new Product("Laptop computer", 344.78m));
            products.Add(new Product("Bubble gum", 2.00m));
            products.Add(new Product("Toothpaste", 6.99m));

            // Create list of categories
            var categories = new List<Category>();
            categories.Add(new Category("Electronics"));
            categories.Add(new Category("Household"));
            
            // Return view
            return View( new ProductViewModel(products, categories));
        }

    }
}

Listing 6 –Views\Product3\Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Controllers.ProductViewModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    <h2>Categories</h2>

    <% foreach (var item in Model.Categories) 
       { %>
    
    
        <li> <%= item.Name %> </li>
    
    
    <% } %>



    <h2>Products</h2>

    <% foreach (var item in Model.Products) 
       { %>
    
    
        <li> <%= item.Name %> </li>
    
    
    <% } %>

</asp:Content>

You can create the view in Listing 6 by selecting ProductViewModel as the data class when adding the view (see Figure 2).

Figure 2 – Adding a strongly-typed view with a ProductViewModel

clip_image004

The pattern that I like here is the pattern of defining the view model in the controller class. The view model is directly related to the controller actions, so it makes sense to define the view model here (in the past, I had created view model classes as separate classes in my Models folder).

If you liked this blog post then please Subscribe to this blog.
posted on Monday, April 13, 2009 8:43 AM | Filed Under [ ASP.NET ASP.NET MVC Tips ]

Comments

Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by rickj1
on 4/13/2009 9:14 AM

I'm working through the Nerd Dinner tutorial it is a good hands on beginners like me need hands on examples to bring things into focus
it is unedited so be sure to download the sample to refer to. MVC Unleashed is not being
realesed untill july 24 and I'am following it's development Being a self tought novice new too programing I only have ASP.NET to compare to I thingk MVC is going to be widly used I have two questions about MVC Unleashed
storing displaying pictures to and from a database and the ability to tie a MVC, Dynamic data and a ASP.NET application wiht some silverlight to pretty it up all together in one app so we can have the best of all four worlds you should devote a chapter to this
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Paul Jaime
on 4/13/2009 10:55 AM

Very interesting and I am sure I we can use your approach on some of our new development efforts. Thanks!.

BTW, what do you use to have code formatted with line numbers, like highlight, and color coding? I could use something like this for our programming documentation.

Thanks,
-Paul
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Casey
on 4/13/2009 11:35 AM

Well, all that for "you can keep ViewModel classes in the same .cs file is a little much explanation" ...

Even ReSharper will suggest you sort that smell out, hit Alt-Enter and it will go to it's own file.

If you want your ViewModels separate from your Models namespace (though in a well structured MVC application the Models directory would only contain ViewModels) then how about just putting i na ViewModel namespace

And if you want them together with the Controller, can I suggest a nested class, which truly achieves what you want.

Putting things into the same CS file is fine for a tutorial app like NerdDinner ... but totally inappropriate for an OO language, or any production code.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Hadi Hariri
on 4/13/2009 11:43 AM

I concur with Casey. The Models folder inside the MVC app should exclusively be for View Models.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by brad
on 4/13/2009 12:11 PM

The untyped view dictionary crap is something they should have left off. Its bad practice in any light.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by achu
on 4/13/2009 12:28 PM

thanks.
I don’t know it is appropriate place to ask but you might know the answer
I want to pass an input control value (say textbox1.value) to a controller action method (as a parameter) without a form post (using Ajax.ActionLink). please see the code below.

I think we cannot put new {name = textbox1.value} in Ajax.ActionLink.


<% =Ajax.ActionLink("mylink", "linkfunction", new {name = textbox1.value}, new AjaxOptions { UpdateTargetId = "result"})%>


and controler action is ..
public string linkfunction(string name)
{
return name;
}
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by achu
on 4/13/2009 12:31 PM

oops html!!

thanks. I don’t know it is appropriate place to ask but you might know the answer
I want to pass an input control value (say textbox1.value) to a controller action method (as a parameter) without a form post (using Ajax.ActionLink). please see the code below.

I think we cannot put new {name = textbox1.value} in Ajax.ActionLink.
/*
input type="text" id="textbox1"
Ajax.ActionLink("mylink", "linkfunction", new {name = textbox1.value}, new AjaxOptions { UpdateTargetId = "result"})
span id="result"

and controler action is ..
public string linkfunction(string name)
{
return name;
}
*/
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Stephen.Walther
on 4/13/2009 2:22 PM

@Paul -- I use the free Syntax Highligher for all of the code listings: http://code.google.com/p/syntaxhighlighter/

Highly recommended.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Andrei Rinea
on 4/13/2009 2:58 PM

I don't get it why you can place HTML in the comments and work :



And I agree with Brad regarding the untyped/dictionary-style ViewData... It leads to un-refactorable code.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by PK
on 4/13/2009 5:22 PM

I use View Data Models (VDM's) as much as possible. The more something is strongly-typed, the less room for erros (eg. typo's in magic names/number, etc).

The difference with what i do and compared to others (i'm finding) is that i'm put the VDM's in the same folder as the view, because i base the VDM off them.

for example.
\Views
\Posts
\Index.aspx
\IndexViewData.cs
\Create.aspx
\CreateViewData.cs

Not every view requires a VDM. But if it did, that's where i put it. Because of the way i name my views, i can't put the VDM's in the root Model folder because of
a) naming conficts.
b) it will get tooo busy. (eg. PostIndexViewData.cs, AccountIndexViewData.cs .. eekks.)

Well, that's what i'm doing. I doubt if anyone else will like it but it's worth putting out there.

-PK-
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by PK
on 4/13/2009 5:24 PM

Ack :( my formatted didn't work in my previous reply.
i'll try again
\Views
....\Posts
........\Index.aspx
........\IndexViewData.cs
(etc)

-PK-
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Farrio
on 4/13/2009 7:00 PM

I'm using typed view model in my project as possible as I could. And I think all views should have this. Untyped view data is a good thing to demo but not to a product and I think MS should move it out.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Mark
on 4/14/2009 2:07 AM

Hi - sorry for such a basic question - but is putting code within your markup, not a step backwards, rather than 'full separation' which was being promoted over the last 7/8 years or so?

Thanks, Mark
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Mr Anonymous
on 4/14/2009 3:36 AM

I kind a feeling smart now, cause noticed this pattern weeks ago.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Adrian Grigore
on 4/14/2009 11:09 AM

I also find this pattern highly useful, but have still not figure out how to use it for an "Edit" action method. The problem is that POST action method always receives a ViewModel with NULL values, no matter what I enter in the view before submitting.

I've written all the details at stackoverflow.com/.../strongly-typed-viewmodel-...

Does anyone have some sample code that shows how this pattern would work with a POST action method?
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Deepak Chawla
on 4/14/2009 11:53 AM

Hi Stephen,
Your email section of the website does not seem to be working. Hence I'm adding this to the post.

Regards,
Deepak Chawla
---------------
Hi Stephen,
I have searched and asked quite a few places a simple question which is

Is there a path where MVC and RIA meat?

Is the view generation in MVC customisable to actually create a silverlight form..

Now that would be a proper division of concerns with rich UI.

I have asked Scott Guthre and also Nikhil Kothari and a few others on their blogs but no answer.

Thanks.

Regards,
Deepak Chawla
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by boleh
on 4/14/2009 11:03 PM

i cannot understand... this is code html or php
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by David
on 4/15/2009 10:00 PM

Thanks so much! Helped me get the typing right that I used for a partial view.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Pravesh
on 4/16/2009 10:38 AM

Good tip again from you.. It is very helpful for me in my development. However I'd like to know that when you are coming up with the new suppliment on asp.net MVC framework book..

Again Thanks for your all tips...
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by psanders
on 4/17/2009 11:21 AM

Unfortunately, nothing new for me.
Where are the other 49 tips, realy interested in ready them.
Send me an email. thanks
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Areg Sarkissian
on 4/17/2009 9:52 PM

Since the models folder
should be a place to only store view models, with your domain models residing in their own business layer project (referenced by the MVC web site project), why not make the structure of the Models folder the same as the Views folder, where when we add a new controller to our project, a subfolder for that controller is created in the Models folder and also have a shared subfolder in the Models folder for any view models that are shared by multiple controllers. This way view models can have their own home, just like controllers and views do.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Areg Sarkissian
on 4/17/2009 10:14 PM

Here is a folder structure example related to my previous comments:

Views--MyController--Index.aspx
Views--MyController--Create.aspx
Views--Shared--Login.ascx

Models--MyController--IndexViewData.cs
Models--MyController--CreateViewData.cs
Models--Shared--LoginViewData.cs

I guess if any one likes this pattern they can build T4 templates for visual studio to
create a subfolder when they add a controller, and add a model class when they add an action to the controller.
Note also since the view model classes are scoped by subfolder namespace there will not be any class name clashing when two different controllers have an action with the same name.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Adrian Grigore
on 4/18/2009 4:48 AM

Stephen, thanks for the great article! It took me a while how to make model binding work for POST action methods, but I finally got it.

In case you are also wondering how to use this technique with POST action methods, you might also want to read my article on this:

stephenwalther.com/.../...-create-view-models.aspx
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Adrian Grigore
on 4/18/2009 4:49 AM

Ooops... wrong link pasted... I meant this: URL:

devermind.com/.../aspnet-mvc-using-custom-viewm...
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Chris
on 4/18/2009 9:41 PM

ViewDataModel makes some sense to me for pages that have a model. What about pages that have no model at all?

IMHO, these ViewData.Model properties might not belong to the model at all.

Take a product detail page that takes a Product. ViewData.Model is a Product, or that's how most tutorials express it. What about app level things, like page title or a site name or global email? ViewData.Model.Title implies it's the models title (Product.Title), not the Page Title or any other title.

I think ViewDataModel blurs the spirit of what what ViewData,Model is, or appeared to be: the object for the main focus of the action. A Product for a detail page, a list of Products for a products page, etc. Other data should be at the ViewData level, not at the ViewData.Model level.

What I tend towards is an AppData : ViewDataDictionary class and set it to the ViewData property in the controller constructors.

This means I have to also set the page inherits correctly.

I've also seen people bypass both methods and just user helpers in the view.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Chris
on 4/18/2009 10:08 PM

Or, in other words...

Some say to never use the name/value key/pairs directly at all. So, if we're going to stuff everything into a ViewDataModel, as ViewData.Model.EveryOtherProperty, it seems we should just ditch the ViewData.Model property altogether and go with custom ViewData classes in some future version.

This particular topic seems like a sharp corner to me in 1.0. Of course, if we have Extension Properties to add to ViewDataDictonary, all would be solved as well.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Chris
on 4/18/2009 10:13 PM

I'[ll stop commenting on myself. I promise. A better example:

ViewData.Model.PageSize
ViewData.Model.PageNumber

this would be a better goal to my eyes:

ViewData.PageSize
ViewData.PageNumber
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by gabotmetal
on 4/19/2009 10:29 PM

this article make me headache emm thank u for being this good info :)
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Chris
on 4/20/2009 6:33 AM

I went back and took a look at the Preview 3 time frame. Now I understand my confusion.

The old ViewData WAS the Model before there was a model. It could be subclassed and it was passed directly to RenderView.

Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by J Stroud
on 4/22/2009 10:51 AM

Stupid question, probably, but whenever I try and add a strongly typed view in the "Add View" dialog, the "Add" button gets disabled. What's more, my model classes aren't showing up in the dropdown.

Any ideas?
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Anonymous
on 4/23/2009 7:04 AM

Yea, I do not get why MS (not necessarily this post) continually push examples or documentation using poor practices. Examples: Datasets, untyped dictionaries, you name it. I don't care if it's for demo purposes or not. What you preach says a lot about your products and work ethics. I don't think DataSets, Entity framework, or things like this should ever have been invented by Microsoft. People need to learn how to code! Pushing bad practices by pushing poor packaged canned objects that go against separation of conerns etc. is just very poor overall as a company by Microsoft.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Anonymous
on 4/23/2009 7:06 AM

JStroud,

Why is that a "stupid question"? I get tired of the development community expecting every developer to just "figure it out and do not ask questions". Most developers do try to figure things out. So no need to say "stupid question", just fire away. It's a simple question but of course most things even architect leads have problems with are those "simple things". We as developers should not have to feel that we have to preface questions with "sorry" or "this is stupid but". Fuck the other developers and just ask.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Stephen.Walther
on 4/23/2009 7:38 AM

@Stroud -- That is not a stupid question -- I got confused about it myself. You need to do a build of your project (select Debug, Build Solution) before opening the Add View dialog. Otherwise, the data classes won't appear in the dropdown list. The Add button is disabled until you pick a data class.

Hope that helps!
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by J Stroud
on 4/24/2009 11:15 AM

Stephen.Walther:
Well, that's intuitive! Thanks for the input.

It's one of those things that NO tutorial mentions.

There are no stupid questions, of course. Only stupid answers. ;)

Anon:
"We as developers should not have to feel that we have to preface questions with "sorry" or "this is stupid but". Fuck the other developers and just ask. "

Brilliant. Just brilliant. Considering that 90% of our communications workload is asking questions, you are absolutely spot on.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Imho
on 4/25/2009 10:36 AM

Just want to test javascript here :)
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by rickj1
on 4/26/2009 11:48 AM

Speaking of questions I have one that I can't seem to find an answer to I've asked on a number of fourms I'm doing Nerd Dinner for the secound time I'm up to chapter 9 in your new book watched all the videos and I'm going to work on your contact manager I'm am trying to put together a MVC site of my own for practice
standard web design membership, profile with picture in database picture tied to Guid UserId tow things need to be done return all the profiler for browsing and return single user based on logged on user for editing Is there a good example, artical are you going to include this in your book maybe write a blog post showing how to do it I've tried every sujestion every way I can think of and I cant get it to come out
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by sheraz
on 5/16/2009 3:57 AM

i have a question about the re usability of viewusercontrols .As we know that we can make a viewusercontrol and place all the view rendering info in it.but still the controller of page is responsible to get the data.So, this means that if i use this control on different pages than i will have to write the Controller code in every function that i create for that particular page.
please advise that if there is any other way to increase the viewusercontrol reusability.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Anonymous
on 6/17/2009 8:48 AM

So you're placing the ViewModel class in the same namespace as your controller rather than creating a separate class for the ViewModel. Is that just preference (organization of structure to keep similar things in the same place physically) or what? I would lean to creating an entire .cs for each ViewModel class as it could grow a bit. Ideally you probably shouldn't have a super long ViewModel class but I don't know, throwing that into your controller might cloud up things? Maybe I'm being too picky.

Opinions anyone?
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
on 7/2/2009 9:59 PM

I wasn't aware of Nerd Dinner application. Thanks for putting the source here.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Svevarn
on 8/4/2009 4:09 AM

Very nice article.

One thing I wonder about.... how to add paging so it still looks pretty as listing 5? Tried to apply "a-la Nerddinner" but doesnt quite work. Always the "excpected model type blah blah".
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Bob
on 8/11/2009 4:06 PM

OK, I see how you get two different tables on same view but i'm having trouble getting two related tables in a view. How do I just get the product rows to show with the product category for each product in the appropriate row? Sorry if I'm missing something easy.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Bob
on 8/12/2009 9:41 AM

I think I've answered my question:

1. Added new class containing product properties and category name.
2. Added new method to repository that gathers product properties and category name via linq join.
3. Changed controller to use the new method.
4. Changed view to use new class.

similar method at: stackoverflow.com/.../how-to-create-a-strongly-...

Regards, Bob
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
on 8/20/2009 2:05 PM

Great article and a nicely written version of the video tutorial. I do have just one thing to say and that is, I love the MVC framework although I must admit, it does feel like a step away from some of the key components within the .NET framework. I understand why, but for example, will server controls become obsolete?
Thanks,
James
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Joel
on 9/9/2009 2:54 PM

What an astounding amount of spam comments on this post. Clever of them to use the URL field and post generic "way to go" messages as the actual comment, I guess. Something tells me the CAPTCHA on this site is a bit broken, though.
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by Trin
on 9/22/2009 11:41 AM

Steve, Your explanation of View Models is very good. I am using .NET 3.51, and I am trying an example with 3 tables with columns as follows

CAR - (carid, ownerid, carvalue)
OWNER - (ownerid, owner_name)
CAR TYPE - (cartypeid, cartype_desc)

1. In a view, I need to display the list of cars AND car type desc for an Owner

2. How do I update Car ?
Gravatar
# re: ASP.NET MVC Tip #50 – Create View Models
Posted by DSD-SICO
on 9/24/2009 4:18 PM

I really like the idea of having the notion of ViewModels and Domain models. However, where would you put your validation code? Simply in the ViewModel, or just in the Domain Model, or in both places? Does anyone have recomendations on that?
Comments have been closed on this topic.