We have an MVC4 ASP.Net site that we are trying to use reflection to loop through properties of a model, and display the names/values/and other information using Html helper
You don't need to use generic implementations for that (EditorFor
, DisplayFor
...), just use the non generic ones, Editor
, Display
...
This will work just fine, you will get validation, automatic bindings and everything else, the whole nine yards...
@foreach (PropertyInfo prop in Model.GetType().GetProperties())
{
@Html.Label(prop.Name)
@Html.Editor(prop.Name)
@Html.ValidationMessage(prop.Name)
}
If you want to experiment with generic implementations here is a great blog post on how to do that
http://www.joelscode.com/use-mvc-templates-with-dynamic-generated-types-with-custom-htmlhelper-extensions/