I have a lot of repeated code in my asp.net MVC views
<div class="editor-label">
@Html.LabelFor(model => model.MyProperty)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MyProperty)
@Html.ValidationMessageFor(model => model.MyProperty)
</div>
I want to wrap this in something like:
foreach (System.Reflection.PropertyInfo item in Model.GetType().GetProperties())
and reuse the same partial view throughout. But I can’t get the syntax quite right.
How can I make this more generic?
This is what Display and EditorTemplates are for.
You need only do
@Html.EditorForModel()and then define a generic template for your model.