我有一個模型:爲什麼我的自定義DisplayFor模板實際上不在這裏呈現?
public class PostModel
{
public int PostId { get; set; }
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Required]
[Display(Name = "Idea")]
[DataType(DataType.MultilineText)]
public string Body { get; set; }
[Required]
public int UserId { get; set; }
public string UserName { get; set; }
}
,我已經創建了一個自定義顯示在/Shared/DisplayTemplates/PostModel.ascx
。然後我就Home/Index
視圖中顯示完整的列表:
@foreach (var p in Model.Popular)
{
Html.DisplayFor(m => p);
}
當通過視圖步進,如果我步入的Html.DisplayFor
,我進入/Shared/DisplayTemplates/PostModel.ascx
連帶如預期進行處理。然而,它從來沒有真正得到渲染。
我錯過了什麼?
這裏是模板:
<%@ Control Language="C#"
AutoEventWireup="true"
CodeBehind="PostModel.ascx.cs"
Inherits="System.Web.Mvc.ViewUserControl<TPB.Models.PostModel>" %>
<div class="rounded-border">
<h2><%= Html.DisplayTextFor(m => m.Title) %></h2>
<p><%= Html.DisplayTextFor(m => m.Body) %></p>
</div>
從我的理解中,您不能將Razor與經典引擎混合使用。 – Aron
@Aron,這實際上是'DisplayFor'和'EditorFor'方法的工作方式。他們尋找'ascx'文件。我只是想念'@'。 –
您正在混合Classic ViewEngine和Razor View Engine。你確定'HtmlHelper.DisplayFor'和'HtmlHelper.EditorFor'應該解析正確的模板,它會在'Views/Shared/DisplayTemplates/{classname}。{extension}'中查找模板。它不應該在視圖引擎之間移動。 http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx – Aron