在 mvc 中使用 Html.DisplayFor 和模板
我有一个模型
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 应显示使用部分视图中指定的模板显示的每个人,但事实并非如此。感谢您的帮助
I have a model
public class Person {
public string Name { get; set; }
public int Age { get; set; }
}
and a Simple View
<%@ 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>
With a Partial View
<%@ 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>
and single Controller
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 });
}
The Displayfor in the view should show each person displayed with the Template specified in the partial view, but it is not. Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分视图名称必须为
Person.ascx
放置在shared\DisplayTemplates
文件夹中The partial view name must be
Person.ascx
placed inshared\DisplayTemplates
folder