Home  /  Questions  /  Question

50   50
Jul 19, 2011

MVC 3 strongly typed View in which ViewModel is a list

I am using MVC 3 Razor and have a ViewModel that itself is a list of objects, say List<Photo>.
How do I declare this at the top of the view?  I.e. what does PhotosModel look like?

@model Site.Areas.Account.Models.PhotosModel

By the way, I know I can nest the list within a ViewModel, but it is the only property, so I would rather not do that.

Is this possibly the solution?

public class PhotosModel : List<Photos>


588   99.9
Jul 24, 2011
You should handle this differently.  Having a view model is a best practice, but you should instead make a property that is of type List<Photo>.  If you also have a property for a single Photo object, then you can use this view model for both lists, and details.  And your page declaration can remain as you have shown.

public class PhotosModel  
   public List<Photo> PhotoList { get; set; }  
   public Photo Photo { get; set; }  

I hope this helps.