自定义EditorTemplate是否可以为其中的相同模型调用默认的EditorTemplate

时间:2011-12-21 19:59:35

标签: asp.net-mvc asp.net-mvc-3

自定义EditorTemplate可以为其中的同一模型调用默认的EditorTemplate吗?同样适用于DisplayTemplates。这是一个简化的例子。在渲染LabelForModel时,不会渲染DisplayForModel或EditorForModel。

查看

<div class="highlight1">
    @Html.DisplayFor(m => m.NullableProp, "NullableIntType1View")
</div>
<div class="highlight2">
    @Html.EditorFor(m => m.NullableProp, "NullableIntType1View")
</div>
<div class="highlight1">
    @Html.DisplayFor(m => m.NullableProp, "NullableIntType2View")
</div>
<div class="highlight2">
    @Html.EditorFor(m => m.NullableProp, "NullableIntType2View")
</div>

共享/ DisplayTemplates / NullableIntType1.cshtml

@model System.Int32?

This is display for NullableIntType1
<hr />  --> @Html.EditorForModel()  <--
<hr />  --> @Html.DisplayForModel() <--
<hr />  --> @Html.LabelForModel()   <--

共享/ EditorTemplates / NullableIntType1.cshtml

@model System.Int32?

This is editor for NullableIntType1
<hr />  --> @Html.EditorForModel()  <--
<hr />  --> @Html.DisplayForModel() <--
<hr />  --> @Html.LabelForModel()   <--

共享/ DisplayTemplates / NullableIntType2.cshtml

@model System.Int32?

This is display for NullableIntType2
<hr />  --> @Html.EditorForModel()  <--
<hr />  --> @Html.DisplayForModel() <--
<hr />  --> @Html.LabelForModel()   <--

共享/ EditorTemplates / NullableIntType2.cshtml

@model System.Int32?

This is editor for NullableIntType2
<hr />  --> @Html.EditorForModel()  <--
<hr />  --> @Html.DisplayForModel() <--
<hr />  --> @Html.LabelForModel()   <--

1 个答案:

答案 0 :(得分:2)

asp.net MVC中的显示和编辑模板Cannot be nested更糟糕的是,执行时,它们只是默默地失败。我认为这是设计模式的巨大限制,也是实施的巨大弱点。它就像魔术一样,但是当它不起作用时,它真的是一个调试的熊。

相关问题