2012-08-16 33 views
3

我在ASP.NET MVC應用程序中爲不同的數據類型(字符串,日期時間等)使用了許多編輯器模板。我現在用舉例來說,下面是字符串:EditorForModel - 使用編輯器時的重複標籤

<div class="editor-label"> 
    @Html.Label("") 
</div> 
<div class="editor-field"> 
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text", placeholder = ViewData.ModelMetadata.Watermark }) 
    @Html.ValidationMessage("") 
</div> 

我想我的觀點中使用EditorForModel但是當我做到這一點,似乎增加它自己的標籤,每個屬性導致重複的標籤(因爲我在我的字符串編輯器模板中有一個標籤)。有沒有什麼辦法,除了從我的字符串editortemplate(在這個例子中)刪除標籤,我可以告訴editorformodel不要插入標籤?

回答

7

一種可能性是覆蓋default object editor template~/Views/Shared/EditorTemplates/Object.cshtml),並刪除它添加標籤:

@if (ViewData.TemplateInfo.TemplateDepth > 1) 
{ 
    @ViewData.ModelMetadata.SimpleDisplayText 
} 
else 
{ 
    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
    { 
     if (prop.HideSurroundingHtml) 
     { 
      @Html.Editor(prop.PropertyName) 
     } 
     else 
     { 
      <div class="editor-field"> 
       @Html.Editor(prop.PropertyName) 
       @Html.ValidationMessage(prop.PropertyName) 
      </div> 
     } 
    }  
} 
+0

工作一種享受 - 謝謝! – 2012-08-16 17:58:27