使用 HTML.Grid 显示子对象
我有一个 ASP.NET MVC 应用程序,它使用单个视图来显示模型实体的属性和子项(及其属性)。
我的模型看起来像这样:
public class Market
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IList<EmailAddress> EmailAddresses { get; set; }
}
public class EmailAddress
{
public virtual int ID { get; set; }
public virtual int MarketID { get; set; }
public virtual string Email { get; set; }
}
在视图上,我想使用表格来显示相关电子邮件地址的列表。为此,我使用 Html.Grid。
<%= Html.Grid(Model.EmailAddresses).Columns( column =>
{
column.For(x => x.Email + Html.Hidden("ID", x.ID)).Encode(false).Sortable(false);
})
.Attributes(style => "width:100%")
.Attributes(id => "emailGrid")
.Empty("There are no Email Addresses set up") %>
但是,当我这样做时,隐藏的 ID 是父实体 Market 的 ID,而不是 EmailAddress 的 ID。
我该如何补救?
I have an ASP.NET MVC application that is using a single view to display the properties and children (with their properties) of a model entity.
My model looks something like this:
public class Market
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IList<EmailAddress> EmailAddresses { get; set; }
}
public class EmailAddress
{
public virtual int ID { get; set; }
public virtual int MarketID { get; set; }
public virtual string Email { get; set; }
}
On the view, I want to use a table to display the list of related email addresses. To do this, I am using Html.Grid.
<%= Html.Grid(Model.EmailAddresses).Columns( column =>
{
column.For(x => x.Email + Html.Hidden("ID", x.ID)).Encode(false).Sortable(false);
})
.Attributes(style => "width:100%")
.Attributes(id => "emailGrid")
.Empty("There are no Email Addresses set up") %>
However, when I do this, the hidden ID is that of the parent entity Market, not that of the EmailAddress.
How do I remedy this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来这可能是 WebGrid 中的一个错误。您是否尝试过重命名 EmailAddress 类中的 ID 字段(例如 EmailID)并将其传递到 WebGrid 并查看它是否正确显示?
It seems it could be a bug in the WebGrid. Have you tried renaming your ID field in the EmailAddress class, e.g. EmailID and pass that to the WebGrid and see if it displays correctly?
这对我有用,难道你的模型填充有问题吗?
This works for me, could it be that you have something wrong in the filling of your model?
由于您在
Column.For()
方法中使用 lambda 表达式,因此 x 参数重新引用单个电子邮件。我认为您的意思是指模型,而不是单个电子邮件 ID而不是执行 x.ID,我认为您只想要 Model.ID
Since you're using the lambda expression for the
Column.For()
method, the x parameter is re referring to a single email. I think you mean to refer to the Model, not a single email IDInstead of doing x.ID, I think you just want Model.ID