【问题标题】:Using Html.DisplayFor in mvc with templates在带有模板的 mvc 中使用 Html.DisplayFor
【发布时间】:2011-02-08 06:43:32
【问题描述】:

我有模特

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

和一个简单的视图

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<Site.Models.Person>>" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
    <% for (int i = 0 ; i < Model.Count ; i++) {  %>
        <%: Html.DisplayFor(m => m[i])%>
    <% } %>
</body>
</html>

局部视图

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Site.Models.Person>" %>
 <h1>A person</h1>
 <div>Name: <%: Html.DisplayFor(x => x.Name)%></div>
 <div>Age: <%: Html.DisplayFor(x => x.Age)%></div>

单控制器

public ActionResult Index () {
        Person p1 = new Person { Age = 18 , Name = "jon" };
        Person p2 = new Person { Age = 23 , Name = "bob" };

        return View(new List<Person> { p1 , p2 });
    }

视图中的 Displayfor 应该显示在局部视图中指定的模板显示的每个人,但事实并非如此。感谢您的帮助

【问题讨论】:

    标签: asp.net-mvc templates html-helper


    【解决方案1】:

    局部视图名称必须是Person.ascx 放在shared\DisplayTemplates 文件夹中

    【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多