创建具有MVC和剃刀的object.cshtml编辑模板 [英] Creating an object.cshtml editor template with MVC and Razor
本文介绍了创建具有MVC和剃刀的object.cshtml编辑模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我期待建立一个编辑模板Object.cshtml改变Html.EditorForModel()方法的行为。我找不到这个使用剃刀的例子。我见过<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html\">this例如使用MVC2和Web窗体视图引擎,但不知道有足够的了解剃刀转换。即使是一个简单的例子是非常有益的。
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.
推荐答案
我只是做显示模板,并留下其余的作为练习读者:)
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>
}
这篇关于创建具有MVC和剃刀的object.cshtml编辑模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文