我希望为 Object.cshtml 创建一个编辑器模板来更改 Html.EditorForModel() 方法的行为。我找不到任何使用 Razor 的示例。我见过这个例子 http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html使用 MVC2 和 WebForm 视图引擎,但对 razor 的了解不够,无法对其进行转换。即使一个简单的例子也会非常有帮助。
我将只做显示模板,其余的留给读者作为练习:)
@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>
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)