Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Wrap Your EditorTemplates

A really cool feature in Razor is the EditorTemplate's ability to use Layouts. Just like regular Views, you can apply the a Layout. For example, a regular Textbox template:

@model string
@{
Layout = "_Layout.cshtml";
}
@Html.TextBoxFor(x => x)
view raw gistfile1.cs hosted with ❤ by GitHub

Would then be used with _Layout.cshtml:

<div class="editor-label">
@Html.LabelFor(x => x)
</div>
<div class="editor-field">
@RenderBody()
@if (ViewData.ModelMetadata.IsRequired)
{
<text><span class="required">*</span></text>
}
@Html.ValidationMessageFor(x => x)
</div>
view raw gistfile1.cs hosted with ❤ by GitHub

3 Comments

Comments have been disabled for this content.