Is it safe to pass an anonymous MVC type ViewData.Model?

Is it possible to do the following:

View(new {Object A, Object B})

      

Or should object A and object B be explicitly declared in the new type?

Thank.

+1


source to share


4 answers


While anonymous types are generic for many MVC purposes, in this case I would either use a regular named class or push a dictionary (or inline). Otherwise, you will have to use reflection / TypeDescriptor

to get the values ​​again.



+2


source


Yes, it is normal. You can use ViewData.Eval ("PropertyName") to get the values ​​and the existing Html helpers will do just fine with that. The only thing you can't do is get strongly typed property access with <% = ViewData.Model.PropertyName%>



+4


source


By skipping anonymous types, you cannot have strongly typed views. You will also need to use reflection in your unit tests.

+2


source


I believe you want to at least give them names:

var model = new 
{
    ObjectA = new A(),
    ObjectB = new B(),
};


view(model);

      

+1


source







All Articles