Back to list
Views:   12K
Replies:  1

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>

Sunil Patel, Jul 19, 2011
Reply 1
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.
King Wilder, Jul 24, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.