我正在尝试实现一些自定义 EditorTemplates,但它们仅由我的 Create 视图呈现,而不是 Edit 视图。
模型
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>
}
有趣的是,如果我使用EditorFor
on Edit.cshtml
thenHtml.cshtml
实际上是渲染的。例如
@Html.EditorFor(model => model.Content)
更新:如果我删除object.cshtml
thenHtml.cshtml
也会正确呈现。所以这似乎是一个问题Object.cshtml
。它在一个视图上工作但在另一个视图上工作似乎很奇怪