ASP.NET MVC 5 Editor Templates

276 Views Asked by At

I'm trying to build some semi-generic editor templates to reduce the amount of repetitive layout HTML I write, i.e., handling label, input, validation message, etc. I want to use them with EditorFor like this:

@Html.EditorFor(m => m.Property, "MyLayoutTemplate")

I want to specify the template explicitly, as any UIHInt attribute for the property should still be used.

A simplified layout editor template could look like this:

<div class="form-group")>
    @Html.Label(ViewData.ModelMetadata.DisplayName, new {@class = ... })
    <div class="...">
        [EditorHere]
        @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName, string.Empty, new { @class = "text-danger" })
    </div>
</div>

I want to have the editor replace the [EditorHere] marker above, i.e., I would like to invoke the EditorFor method somehow. Is it possible?

Invoking EditorFor(m => m) does not work regardless of whether the template is typed or not. However, I think this must be achievable somehow as the ViewData.ModeMetadata seems to contain all the required information.

I know how to achieve this using an HtmlHelper extension method, and I have already done so. However, I do not really like building HTML in code.

This question is basically the same as Extending Asp.net MVC 3 Editor Templates However, I cannot use the answer, as I do not want to use th default editor.

0

There are 0 best solutions below