Search code examples
c#asp.net-mvc-3viewmodelfill

How to populate a ViewModel in ASP.NET MVC3


In my Controller I have a ProductInfo class from my Domain Model and I need some of its information to populate my View Model ProductStatsVM.

How do you populate the View Model? I heard three possible ways:

  1. Populate the View Model directly from the Controller (not good, I want to keep my Controller slim)
  2. By using a View Model constructor and pass the domain model as parameter. (I have to create a constructor for each domain model class I want to use)
  3. By using a Fill() method. (I saw it on the web, no idea how it works I guess this way the ViewModel should be aware of the Service Layer and creates coupling).

I know there are tools like AutoMapper, which I am going to use indeed, but before I want to understand the logic on how to fill a View Model from the Controller without using any additional tool.


Solution

  • The idea is that your controller action queries some repository to fetch a domain model. Then it passes this domain model to a mapping layer which is responsible to convert it to a view model and finally it passes the view model to the view:

    public ActionResult Index(int id)
    {
        ProductInfo product = repository.GetProductInfo(id);
        ProductViewModel viewModel = Mapper.Map<ProductInfo, ProductViewModel>(product);
        return View(viewModel);
    }
    

    and you could even make your controller slimmer by introducing a custom action filter that will automatically intercept the Model in the OnActionExecuted event and call into the mapping layer to substitute it with the corresponding view model so that your controller action now becomes:

    [AutoMapTo(typeof(ProductViewModel))]
    public ActionResult Index(int id)
    {
        ProductInfo product = repository.GetProductInfo(id);
        return View(product);
    }
    

    and of course now the view is strongly typed to ProductViewModel:

    @model ProductViewModel
    ...
    

    Up to you to implement the Mapper.Map<TSource, TDest> method. And if you don't want to implement it yourself you could download AutoMapper which already has this method for you.

    The mapping layer is something that is part of the MVC application. It must be aware of both the domain models coming from your service layer and the view models defined in your MVC application in order to be able to perform the mapping.

    Don't use constructors (other than the default parameterless one) in your view models. The default model binder will choke if the view model doesn't have a parameterless constructor in your POST actions and you will have to implement custom model binders.