2011-12-21 28 views
4

能自定義EditorTemplate調用默認EditorTemplate對裏面的同型號?同樣適用於DisplayTemplates。這是一個簡單的例子。在呈現LabelForModel時,不呈現DisplayForModel或EditorForModel。能自定義EditorTemplate調用默認EditorTemplate同型號裏面

查看

<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() <-- 

回答

2

在asp.net MVC Cannot be nested更糟的顯示和編輯模板,執行時,他們只是默默地失敗。我認爲這是實現的設計模式和巨大的弱點的巨大的限制,以及。它的功能就像魔術一樣,但當它不起作用時,它確實是一個可以調試的熊。