Los Techies : Blogs about software and anything tech!

How we do MVC – View models


A while back, I went over a few of the patterns and opinions we’ve gravitated towards on our current large-ish ASP.NET MVC project, or, how we do MVC.  Many of these opinions were forged the hard way, by doing the wrong thing many times until we found the “right” opinion.  Of course, many of these opinions are only really valid in the constraints of our project.  While the domain of this project isn’t important, here are some key aspects to consider:

  • AJAX is used very, very sparingly.  Section 508 compliance is required
  • XHTML compliance is also required
  • XHTML DTD validation is also required
  • All (well, 99%) operations revolve a single uber-entity.  Think customer relationship management, where everything you do deals with exactly one customer
  • Snippets of information repeated across many screens
  • Screens are either edit, or view, but never both.  (99% never)

Given these constraints, these opinions may or may not apply to the project you work on.  Again, patterns are all about tradeoffs, benefits and liabilities.  But, opinionated software is like building a bullet train.  It goes extremely fast, but only in the direction you build it.

That said, I’m going to go over some of the main aspects of our MVC usage in a series of posts – starting with ViewModels.

ViewModel design

For our application, the ViewModel is a central aspect of our MVC architecture.  One of the first dilemmas facing MVC developers is to decide what the “M” in MVC means in ASP.NET MVC.  In Rails, this is fairly clear, the M is ActiveRecord (by default).  But in ASP.NET MVC, the “M” is silent!  Its out-of-the-box architecture offers no guidelines nor advice on what the M should be.  Should it be an entity?  Data access object?  DTO?  Something else?

Sidenote – the term DTO is far overused.  DTO is a Data Transfer Object.  The pattern describes the usage, not the shape of a type.  Just because an object is all properties and no methods does NOT mean it’s a DTO.

For us, the ViewModel is inextricably linked to Views, which leads us to our first rule:

Rule #1 – All Views are strongly-typed

I think I’ve gone over this one enough, as I really can’t stand magic strings and loose contracts.  A dictionary as ViewModel is a very loose contract between the Controller and View.  While on rare occasions we still need to pass information in the dictionary part, we’ve limited this to supporting information to help render some of the lower-level pieces of the View, and are used for some “plumbing” pieces, these pieces do not show up in our Controller action nor are they visible when you design the view.

Rule #2 – For each ViewModel type, there is defined exactly one strongly typed View

We’ll get into how we do this soon, but this rule has a lot of implications:

  • ViewModel types are distinct from our Domain Model types
  • The choice of what View to show can be decided strictly on the shape of your ViewModel
  • Re-used pieces in a View (through Partials) can be decided through re-using ViewModel types

image

On the first point, we never pass an Domain Model entity straight into the view.  Most of the time, we only show a slice of information from a single entity.  And many other times, the same snippet is shown in many places.

Rule #3 – The View dictates the design of the ViewModel.  Only what is required to render a View is passed in with the ViewModel.

If a Customer object has fifty properties, but one component only shows their name, then we create a custom ViewModel type with only those two properties.  Because we only have one ViewModel type per View, we shape our ViewModel around only what is displayed (or used) in that View.  Why is this a Good Thing?

For one, just having a ViewModel points us to the right View.  We need any other information other than your ViewModel type to pick the correct View.  This also means we no longer need to concern ourselves with locating views by some arbitrary name, as the ViewModel is the View.  Things like RenderPartial, which I have to select a name, become rather pointless at that point.

Rule #4 – The ViewModel contains only data and behavior related to the View

For the most part, our ViewModel contains only data.  Most, if not all aggregations/calculations or shaping is done through our Domain Model.  But occasionally, we have View-specific behavior or information, and that rightly belongs on our ViewModel.

We’ve looked at how we design our ViewModel and what it looks like, but how does it get there?  If we create all these distinct ViewModel types separate from our Domain Model, didn’t we just create a bunch of work for ourselves?  We thought so too, which is why we developed AutoMapper on this project.

Building the ViewModel

When we introduced AutoMapper into our MVC pipeline, we had a real problem.  Do Controllers need to do the mapping between Domain Model and ViewModel in each action method?  That becomes rather annoying for unit testing, as the mapping operation could warp things to a state that it becomes difficult to pull things back out.  For example, or EditModels (ViewModels for forms) are very string-y, where DateTimes, Ints, Decimals etc are represented as strings.  This comes from us using Castle Validators (future post, I promise) for validation. 

So more moving parts, a dependency across all controllers?  No, mapping in our Controller action just won’t do.  Instead, we’ll use an Action Filter to do the work for us:

image

A request comes in, handled by an Action.  The Action does its thing, ultimately deciding how to respond to the request.  In many cases, this means rendering a view (ViewResult).  From there, our Action Filter comes into play.  On our Action method, we decorate it with an AutoMap attribute to configure the source/destination type pair to be mapped:

[AutoMap(typeof(Product), typeof(ShowProduct))]
public ActionResult Details(int id)
{
    var product = _productRepository.GetById(id);

    return View(product);
}

Very trivial, yes, but here we see that we still use the strongly-typed version of the View method, so that means that our model on the Action side, which I call the Presentation Model (feel free to pick a better name), is the strongly-typed ViewModel for the moment.  The Presentation Model, which the Action creates, can be an entity, an aggregate root, or some other custom aggregate component that we build up.

From there, we decorated our action with a filter that specified we need to map from Product to ShowProduct.  Why do we have to specify the source type?  Well, many ORMs, including NHibernate, rely on proxy types for things like lazy loading.  Instead of relying on the runtime type, we’ll explicitly specify our source type directly.  This also helps us later in testing, as we can whip through all of our controller actions using reflection, and test to make sure the source/destination type specified is actually configured.

The filter attribute is very simple:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class AutoMapAttribute : ActionFilterAttribute
{
    private readonly Type _sourceType;
    private readonly Type _destType;

    public AutoMapAttribute(Type sourceType, Type destType)
    {
        _sourceType = sourceType;
        _destType = destType;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var filter = new AutoMapFilter(SourceType, DestType);

        filter.OnActionExecuted(filterContext);
    }

    public Type SourceType
    {
        get { return _sourceType; }
    }

    public Type DestType
    {
        get { return _destType; }
    }
}

We simply capture the types and delegate to the real action filter for the work.  This is again because I believe in separating metadata in attributes from the behavior they perform.  Attributes just don’t work well for behavior.  Instead, I’ll create a separate action filter:

public class AutoMapFilter : BaseActionFilter
{
    private readonly Type _sourceType;
    private readonly Type _destType;

    public AutoMapFilter(Type sourceType, Type destType)
    {
        _sourceType = sourceType;
        _destType = destType;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var model = filterContext.Controller.ViewData.Model;

        object viewModel = Mapper.Map(model, _sourceType, _destType);

        filterContext.Controller.ViewData.Model = viewModel;
    }
}

The BaseActionFilter is just a class that implements the various filter methods as virtual members, so I can override just the ones I need to use.  The AutoMapFilter pulls the original PresentationModel out of ViewData, performs the mapping operation, and puts the mapped ViewModel into the ViewData.Model property.  From there, the strongly-typed view for our specified ViewModel type is rendered.

Because AutoMapper can flatten source types, we often find our ViewModel to simply follow a property chain for various pieces of information.  Again, we let our View shape that piece.  If we decide not to flatten, it’s usually because we’re creating a partial that re-uses a ViewModel type across other parent ViewModel types.

Wrapping it up

Designing ViewModels is quite ambiguous with MVC, as the shipped platform doesn’t offer any guidance or opinions in that area.  But by forming rules around our ViewModel, we can create a path and direction for our innovation.  Our rules are designed to strengthen the relationship between the View and the Model, with a concept of ViewModel – a Model designed exclusively for exactly one View.

In the next post, we’ll look at designing our views – for both viewing and editing data, and how we’ve crafted opinionated HTML builders to eliminate a lot of duplication and enforce standardization.

Kick It on DotNetKicks.com
Posted Jun 29 2009, 11:06 PM by bogardj
Filed under: ,

Comments

Posts about Software as of June 30, 2009 | Easy Reach Software wrote Posts about Software as of June 30, 2009 | Easy Reach Software
on 06-29-2009 11:45 PM

Pingback from  Posts about Software as of June 30, 2009 | Easy Reach Software

Jake Scott wrote re: How we do MVC – View models
on 06-30-2009 2:14 AM

It would be really cool if you have a demo project somewhere that we can download, is this in the pipeline? So next up is learning how you post data.

It would be cool to know how you do binding of these view models, validation as well as your approach for mapping these back into models...

Cheers

Jake

Reflective Perspective - Chris Alcock » The Morning Brew #379 wrote Reflective Perspective - Chris Alcock » The Morning Brew #379
on 06-30-2009 3:57 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #379

mick delaney wrote re: How we do MVC – View models
on 06-30-2009 6:30 AM

do u see all these ideas merging into an opinionated framework on top of mvc?

i've been doing some rails dev too and 1 of the strengths is that some of the decisions have been made, i see u are involved in tarantino, but we also have sharparc, mvccontrib etc... will we see something like Fubu mvc on top of asp.net mvc?

le tot wrote re: How we do MVC – View models
on 06-30-2009 8:31 AM

We are doing almost the same, using a view model, our problem is that we have a anemic ddd, we don't know all  the domain, and probably never do (yes, sad but true), to fill   the view model we use a separate class for each view, a task class, that returns the strongly type ViewModel from our domain, , and each task class uses a Map class that do the map (we are doing this to be able to test the maps),  we don't use an attribute based map, but now we will see if that approach can fit.

Regards.-

bogardj wrote re: How we do MVC – View models
on 06-30-2009 9:21 AM

@Jake

Yeah, all of those topics are in the pipeline.  As far as a demo project, CodeCampServer is about as close as it gets out there right now.  It was built with many concepts of our current project, but a lot of different ones too.

@Mick

I doubt it.  Many of our choices were dictated by the constraints of our project, while others we want to apply broadly.

Design of our controllers/actions is probably not going to be lifted to other places, and it's also why I generally won't talk about controller design.  Honestly, a lot of the stuff we did isn't built to be consumed outside of this project.  The concepts, however, are fairly straightforward to re-do.

Mark Keller wrote re: How we do MVC – View models
on 06-30-2009 9:51 AM

We are also doing pretty much the same thing.  The only real difference is that we don't quite do the 1:1 view - viewmodel mapping.  we'll reuse viewmodels for detail views vs. edit/create views.  I like the use of the automapper attribute

The question we are still trying to hash out is where the heck to put the User info such as username, roles, is logged in, etc...?  Are you sticking this in some kind of base viewmodel? separate json calls?    it doesn't feel right to put it into the requested action's viewmodel, but not really sure where else to put it.  I'd love to hear how you are handling that kind of info needed for your views.

bogardj wrote re: How we do MVC – View models
on 06-30-2009 10:58 AM

@Mark

RenderAction is your friend here. It's perfect for orthogonal concerns for a request.  I think of it like chain of responsibility/decorator - a bunch of disparate actions get a crack at a request, pulled together by a master view page directing where the resultant HTML goes.

Travis wrote re: How we do MVC – View models
on 06-30-2009 12:00 PM

How are you handling data that is needed in a ViewMasterPage? I have my own ideas, but I would like to hear yours.

bogardj wrote re: How we do MVC – View models
on 06-30-2009 12:09 PM

@Travis

I really don't like action filters used to supply ViewData - I go for the RenderAction route instead.  I'd also rather not set up some inheritance hierarchy for my ViewModel for information in a master page either.

I know that master pages can be strongly typed, but I don't really know how that would work.

Travis wrote re: How we do MVC – View models
on 06-30-2009 12:21 PM

@bogardj well the inheritance hierarchy is the route I went, and it just does not feel right. It would be interesting if others would chime in.

on 06-30-2009 2:45 PM

ASP.NET MVC/Web Programming How we do MVC - View Models VS2010 Beta 1 Web Application Project Database package and SMO Options ASP.NET 4.0 Enhancements to Data Controls C# Community Convergence LI Connected Systems (Azure, WF, WCF, etc) Windows Azure

jcteague wrote re: How we do MVC – View models
on 06-30-2009 2:50 PM

The fact that you are typically dealing with one entity per action gives you  the ability to leverage AutoMapper.  We tried using AM on our latest proejct, but did not get a lot of mileage from it because almost all of the data we were sending to view came from multiple sources.  It would be cool if I could use multiple sources for AM.  Is that possible?

bogardj wrote re: How we do MVC – View models
on 06-30-2009 2:54 PM

@John

So we have two choices - one is to create an intermediate presentation model to hold everything.  It makes a lot of sense where you have a composite presentation model with behavior or composes information.

Otherwise, I'm looking at a feature for the next AM version to provide the ability to "Fill" a destination object.  It's something we've wanted here and there, but always solved by creating an intermediate model on the controller side.

Arjan’s World » LINKBLOG for June 30, 2009 wrote Arjan’s World » LINKBLOG for June 30, 2009
on 06-30-2009 4:02 PM

Pingback from  Arjan’s World    » LINKBLOG for June 30, 2009

Stephen wrote re: How we do MVC – View models
on 06-30-2009 6:36 PM

Looks similar to what we do, only we didn't use automapping and thus not the implicit mapping of model to viewmodel..

Personally I wonder how that even works, we've found for even simple perf requirements where relational data exists, that a controller action must know far to much than seems 'right' about the view in order that it only retreives the relevant information and selects almost straight into a view model.. we thus only tend to use the 'model' as a concept for the query..

for example:

from p in Products

where ...

select new ProductDetailsView(p.Name, ...);

Things end up messy, but it seems its either messy or has scary performance issues.. selecting an entire model out to give to a view in a blasé 'you pick what you want from this' is hugely unrelealistic.

Not to mention a view might consist of data from multiple 'unlinked' models.. such as.. related products.. which would mean we have:

Product

RelatedProducts

We would need to create a model then that encompasses these two, just so that it can then have certain parts 'picked out' by the mapper, as it maps to the viewmodel..

bogardj wrote re: How we do MVC – View models
on 06-30-2009 7:09 PM

@Stephen

You'd need to couple this with fetching strategies to prevent the problems you're talking about.  But, you'd do this anyway, if it's a "pick and choose" kind of thing.  That's the other way to go - do the flattening/shaping at the query layer.

As for things like "related products", those are some things that I would render using something like RenderAction - as these pieces likely don't change as much, or change via external events.

Either way, I'm still not using a dictionary.  Why use a dictionary when you can create an object with properties?

Ryan wrote re: How we do MVC – View models
on 06-30-2009 9:55 PM

What do you use for modeling when receiving information back from the user (e.g. the the information posted back to a controller action from an edit form?)

Do you take in a Domain Object directly, a 'dumb' InputModel equivalent of the ViewModel and map it back to your domain object, or something else entirely?

I have a much harder time figuring out what to use for input models than output.

Daily Links for Wednesday, July 1st, 2009 wrote Daily Links for Wednesday, July 1st, 2009
on 07-01-2009 7:32 AM

Pingback from  Daily Links for Wednesday, July 1st, 2009

bogardj wrote re: How we do MVC – View models
on 07-01-2009 8:41 AM

@Ryan

The same thing that got pushed out.  Our EditModel is automapped from the presentation model (domain model or otherwise), and is what's posted back.  We then translate EditModels into one or more commands (it's not a task-based UI), and execute the business rules and handler for each command.  I'll touch on that stuff soon too.

Quick note - our edit pages _only_ have input elements, and we generally don't have view and edit information on one screen.

Scott Koon wrote re: How we do MVC – View models
on 07-01-2009 9:27 AM

Given you initial constraints, how many of your views do you think could be code-gen'ed/written as static HTML rather than a dynamic page?

bogardj wrote re: How we do MVC – View models
on 07-01-2009 9:46 AM

@Scott

We actually looked at that...but there were some other constraints on our forms that led us away from that.  Our forms (for a records management app) have dozens of fields, and our designers wanted to be flexible for overall layout.  For us, it was easier to do that in HTML.

jcteague wrote re: How we do MVC – View models
on 07-01-2009 11:44 AM

@Jimmy

What do I gain by AutoMapping an Intermediate model to a ViewModel, when the only purpose of the intermediary is to go to the view?

bogardj wrote re: How we do MVC – View models
on 07-01-2009 11:57 AM

@John

Well, sometimes the intermediate model has a real domain concern.  Other times, it doesn't.  On the latter, I'm looking at extending AutoMapper to fill and compose a larger object.

Otherwise, you just get the flattening benefit - where your view matches 1:1 to a view model.

Brig Lamoreaux wrote re: How we do MVC – View models
on 07-01-2009 12:44 PM

Hi Jimmy,

I like to see how others have been using and thinking about MVC. One thing that we do a little different is not having a 1:1 ViewModel to View relationship.

What I've found is there are many cases where two view share 80 percent of the data. This is especially the case when a workflow leads the user from one view to another. In these cases the views in the workflow share the same ViewModel.

One benefit of doing this is the ViewModel knows how to read and transform Form data into the ViewModel and validate it. Although you mentioned something about EditModels which I would like to know more about.

bogardj wrote re: How we do MVC – View models
on 07-01-2009 8:01 PM

@Brig

Yeah, we definitely don't have any workflows.  Our forms are one and done.  But we've stayed completely away from any kind of interaction with the Forms business, that seems a little messy.

Gino wrote re: How we do MVC – View models
on 07-04-2009 1:46 PM

@ Travis: I did go for ViewModel inheritence as well. However, I don't feel bad about it... You seem to do, could you share why?

I think there's nothing wrong with putting common information (something ALL views that depend on a certain mast page need) in a base class. (typesafe.be/.../the-viewmodel-and-the-masterpage)

Code Monkey Labs wrote Weekly Web Nuggets #70
on 07-05-2009 11:03 PM

Pick of the week: All Abstractions Are Failed Abstractions General Understanding Expression Trees : Marcin Kasprzykowski takes a look at expression trees, which were introduced in C# 3.0. One Public Type Per File : Chris Eargle examines a few of the many

AutoMapper in NerdDinner wrote AutoMapper in NerdDinner
on 07-06-2009 7:02 AM

Pingback from  AutoMapper in NerdDinner

igorbrejc.net » Fresh Catch For July 7th wrote igorbrejc.net » Fresh Catch For July 7th
on 07-07-2009 2:05 PM

Pingback from  igorbrejc.net » Fresh Catch For July 7th

Jim Zimmerman - ASP.NET, MVC, Ajax and ORM wrote IEnumerable Extension Method for AutoMapper
on 07-10-2009 12:38 AM
David Hayden - Florida .NET Developer - C# and SQL Server wrote ASP.NET ActionFilter with AutoMapper and View Model Sample Code
on 07-10-2009 2:16 PM

Sample code from my presentation last night. Thanks for attending!

David Hayden - Florida .NET Developer - C# and SQL Server wrote ASP.NET MVC ActionFilter with AutoMapper and View Model Sample Code
on 07-10-2009 2:22 PM

Sample code from my presentation last night. Thanks for attending!

Daniel Teng wrote 每周链接 #17
on 07-12-2009 12:14 PM

架构和设计

敏捷倡导者和架构师:是同盟而不是对手www.infoq.com/.../agilists-and-architects

释放你的领域http:/...

ASP.NET MVC Archived Blog Posts, Page 1 wrote ASP.NET MVC Archived Blog Posts, Page 1
on 07-12-2009 11:11 PM

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

DotNetShoutout wrote How we do MVC – View models - Jimmy Bogard - Los Techies
on 07-13-2009 6:21 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Summary 14.07.2009 « Bogdan Brinzarea’s blog wrote Summary 14.07.2009 « Bogdan Brinzarea’s blog
on 07-14-2009 4:08 AM

Pingback from  Summary 14.07.2009 «  Bogdan Brinzarea’s blog

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - July 14-17, 2009
on 07-16-2009 3:26 AM

Daily tech links for .net and related technologies - July 14-17, 2009 Web Development ASP.NET Membership

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - July 14-17, 2009
on 07-16-2009 4:12 AM

Daily tech links for .net and related technologies - July 14-17, 2009 Web Development ASP.NET Membership

Nima wrote re: How we do MVC – View models
on 07-18-2009 5:00 AM

Thank you

What about paging?  I wondering why no sample explaining paging and mapping together.  As I remember MVC contrib GridView use IPagedList datasource for paging. It need to map model after data paged by grid.

The Problem with the MVC Frameworks – Introduction | Wizards of Smart wrote The Problem with the MVC Frameworks – Introduction | Wizards of Smart
on 08-05-2009 2:03 PM

Pingback from  The Problem with the MVC Frameworks – Introduction | Wizards of Smart

Twitter Trackbacks for How we do MVC ??? View models - Jimmy Bogard - Los Techies : Blogs about software, programming and anything [lostechies.com] on Topsy.com wrote Twitter Trackbacks for How we do MVC ??? View models - Jimmy Bogard - Los Techies : Blogs about software, programming and anything [lostechies.com] on Topsy.com
on 08-29-2009 6:23 PM

Pingback from  Twitter Trackbacks for                 How we do MVC ??? View models - Jimmy Bogard - Los Techies : Blogs about software, programming and anything         [lostechies.com]        on Topsy.com

Hans wrote re: How we do MVC – View models
on 10-22-2009 8:50 PM

It seems to need add the "Mapper.CreateMap" in OnActionExecuted:

public override void OnActionExecuted(ActionExecutedContext filterContext)

   {

       var model = filterContext.Controller.ViewData.Model;

       Mapper.CreateMap(_sourceType, _destType);

       object viewModel = Mapper.Map(model, _sourceType, _destType);

       filterContext.Controller.ViewData.Model = viewModel;

   }

bogardj wrote re: How we do MVC – View models
on 10-22-2009 9:41 PM

@Hans

Since the CreateMap calls only need to happen once per AppDomain, we do CreateMap calls in Application_Start in global.asax.  Inside the Application_Start, we call into a bootstrapper class that configures our IoC container, ORM, AutoMapper, and other static configuration.

ulu wrote re: How we do MVC – View models
on 10-28-2009 6:21 PM

Do you have separate models for Create and Edit views? For List and Details? That must be a lot of duplication. You mention that you reuse view pieces by using partials, how do you reuse model pieces? Is there a sample of this in the CodeCamp?

ulu

ulu wrote re: How we do MVC – View models
on 10-28-2009 6:24 PM

Do you use separate models for Create and Edit views? For List and Details? Seems to be a lot of duplication here..

ASP.NET MVC View Model Pattern « Vincent Leung's .NET Tech Clips wrote ASP.NET MVC View Model Pattern « Vincent Leung's .NET Tech Clips
on 10-28-2009 8:36 PM

Pingback from  ASP.NET MVC View Model Pattern « Vincent Leung's .NET Tech Clips

bogardj wrote re: How we do MVC – View models
on 10-29-2009 9:34 PM

@ulu

Yes, separate models for View and Edit.  But Edit has its own requirements, even the types might be different.  Throw in validation, it starts to diverge quite a bit.  Not for every application of course, but ours tend to be quite different.

Typically, our View pages show a lot more associations, where the Edit does not.

ASP.NET MVC Auto Model Validation with xVal « Corey Coogan wrote ASP.NET MVC Auto Model Validation with xVal « Corey Coogan
on 10-29-2009 10:56 PM

Pingback from  ASP.NET MVC Auto Model Validation with xVal « Corey Coogan

The ViewModel and the MasterPage « typesafe wrote The ViewModel and the MasterPage « typesafe
on 11-01-2009 2:57 PM

Pingback from  The ViewModel and the MasterPage « typesafe

hotgazpacho » Blog Archive » Weekly Digest for the week ending November 4th wrote hotgazpacho » Blog Archive » Weekly Digest for the week ending November 4th
on 11-04-2009 12:30 AM

Pingback from  hotgazpacho  » Blog Archive   » Weekly Digest for the week ending November 4th

ulu wrote re: How we do MVC – View models
on 11-09-2009 5:47 AM

I was actually asking whether you have your Create models different from the Edit models. Or do you reuse the view for Edit and Create?

Another question is, is your Details model different from the List model? Inherits from it?

Whenever you add or rename or remove some property from your domain class, how do you keep all these models in synch?

bogardj wrote re: How we do MVC – View models
on 11-09-2009 8:11 AM

@ulu

We'll have a single ViewModel per View, so 1:1 relationship there.  In this project, when an edit screen and create screen are exactly the same, we'll use a single ViewModel type.

Omu wrote re: How we do MVC – View models
on 11-09-2009 10:12 AM

how about the problem of selectlist in asp.net mvc, that you need to provid it with the full list of values and the selected values and you get back in the POST only an int (the selected value)

@Omu wrote re: How we do MVC – View models
on 11-09-2009 10:35 AM

let's say we have something like this

public class Person

{

  public string Name {get; set;}

  public Country Country {get; set;}

}

public class PersonViewModel

{

  public Person Person {get; set;}

  public SelectList Countries {get; set;}

}

can automapper be used to perform to parse from Person into PersonViewModel and back ?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems