2013-01-16 80 views
0

我試圖實現一些自定義EditorTemplates,但它們只是由我的創建視圖呈現,而不是編輯視圖。MVC 4 Html.Editor忽略EditorTemplate

型號

public class Page { 
    public int PageID { get; set; } 
    [DataType(DataType.Html)] 
    [AllowHtml] 
    // I tried including [UIHint("Html")] but this made no difference 
    public string Content { get; set; } 
    ... 
} 

/Views/Shared/EditorTemplates/Html.cshtml

@model string 
@Html.TextArea("", Model, new { @class = "html"}) 

/Views/Shared/EditorTemplates/Object.cshtml

@if (ViewData.TemplateInfo.TemplateDepth > 1) 
{ 
    @ViewData.ModelMetadata.SimpleDisplayText 

} else { 

    @Html.ValidationSummary(false) 

    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit 
         && !ViewData.TemplateInfo.Visited(pm))) 
    { 
     if (prop.HideSurroundingHtml) { 
      @Html.Editor(prop.PropertyName) 
      @prop.DataTypeName 
     } else { 
      <div class="form-field"> 
       @if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) { 
        @Html.Label(prop.PropertyName) 
       } 
       @Html.Editor(prop.PropertyName) 
      </div> 
     } 
    } 
} 

/Views/Page/Create.cshtml(這正確呈現Html.cshtml

@model MvcDisplayTemplates.Models.Page 

@using (Html.BeginForm()) { 
    @Html.EditorForModel(Model) 
    <p><input type="submit" value="Create" /></p> 
} 

/Views/Page/Edit.cshtml(這只是呈現默認的單行文本編輯器)

@model MvcDisplayTemplates.Models.Page 

@using (Html.BeginForm()) { 
    @Html.EditorForModel(Model) 
    <p><input type="submit" value="Save" /></p> 
} 

有趣的是,如果我在Edit.cshtml上使用EditorFor,那麼實際上會渲染Html.cshtml。例如

@Html.EditorFor(model => model.Content) 

更新:如果我刪除object.cshtml然後Html.cshtml也是正確呈現。所以這似乎是Object.cshtml中的問題。這似乎只是奇怪的是,它可以在一個視圖而不是另一個

回答

1

我固定的,爲什麼它以前在一個視圖,雖然工作,但沒有其他明確設置模板Object.cshtml

@Html.Editor(prop.PropertyName, prop.TemplateHint ?? prop.DataTypeName) 

尚不清楚。