Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How do I handle a view model with many properties?

So I have an almost 1:1 ratio of views to view models and things seem to be going well. If I understand their purpose correctly it seems that view models should

  1. "Strip down" Entity models so that only relevant properties are passed to the presentation layer
  2. Add additional information needed for presentation such as a list of state abbreviations or contact types when creating, say, an address.

In trying to keep with those principles I've sort of hit a bit of a wall with my Reports controller. Various reports that are generated for a customer require access to about 30 or so different properties. As such, my view model ends up looking very similar to my Entity model.

Of course the easiest solution is to just pass the Entity model to the view so that I'll have access to all properties, however I also need to be able to generate reports for blank or "incomplete" customers. This causes problems will null reference exceptions when trying to access navigation properties on my Entity models.

So I can either use a null check on just about every field within the view, which doesn't seem too appealing... OR I could implement a view model to avoid the null reference exceptions. The problem is that I'd end up with a view model that looked like this:

var customer = customersRepository.GetCustomer(id);
var viewModel = new CustomersViewModel()
{
    FirstName = customer.FirstName,
    LastName = customer.LastName,
    Address = customer.MailingAddress.Address,
    City = customer.MailingAddress.City,
    // and on and on for about 30 different properties
};
return View(viewModel);

Typing all those properties out is one of those things that just feels wrong. Am I missing a more elegant solution to this problem?

like image 739
Jeff Camera Avatar asked Oct 02 '10 16:10

Jeff Camera


People also ask

Can we have 2 models in a single view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.

How many views does the model can have in MVC?

ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible.


2 Answers

The problem is that I'd end up with a view model that looked like this

AutoMapper is a must to avoid writing exactly the code you posted. I would also recommend you watching the excellent put your controllers on a diet video from the creator of AutoMapper. After watching this video (and a bit of an effort from your side) your controller action will be reduced to a pretty one liner.

like image 117
Darin Dimitrov Avatar answered Nov 01 '22 15:11

Darin Dimitrov


You should definitely look into AutoMapper ( http://automapper.codeplex.com/ ).

AutoMapper.Mapper.CreateMap(typeof(CustomersModel), typeof(CustomersViewModel));

AutoMapper.Mapper.CreateMap<CoolObject, CoolObjectViewModel>()
    .ForMember(d => d.Property1, f => f.MapFrom(s => s.Property1))
    .ForMember(d => d.Property2, f => f.MapFrom(s => s.Property2))
    .ForMember(d => d.Property3, f => f.MapFrom(s => s.Property3));
like image 34
Buildstarted Avatar answered Nov 01 '22 14:11

Buildstarted