I am looking to create an editor template for Object.cshtml to change the behavior of the Html.EditorForModel() method. I can't find any example of this using Razor. I have seen this example using MVC2 and WebForm view engine but don't know enough about razor to convert it. Even a simple example would be very helpful.
+1
A:
I'm just going to do the Display template and leave the rest as an exercise for the reader:)
@if (Model == null) {
<text>@ViewData.ModelMetadata.NullDisplayText</text>
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
<text>@ViewData.ModelMetadata.SimpleDisplayText</text>
} else {
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
if (prop.HideSurroundingHtml) {
<text>@Html.Display(prop.PropertyName)</text>
} else {
<tr>
<td>
<div class="display-label" style="text-align: right;">
@prop.GetDisplayName()
</div>
</td>
<td>
<div class="display-field">
@Html.Display(prop.PropertyName)
</div>
</td>
</tr>
}
}
</table>
}
marcind
2010-10-27 02:28:15
Thanks for this, but when I paste this code into my object.cshtml I get "error CS1024: Preprocessor directive expected"
Craig
2010-10-27 02:51:33
Doh, your code is correct. I accidentally had #Html.EditorForModel() rather than @Html.EditorForModel(). At least your code is not wasted and will be first seen by many new Razor users in the future.
Craig
2010-10-27 02:53:58