使用 HTML.Grid 显示子对象

发布于 2024-12-19 08:41:13 字数 930 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江城子 2024-12-26 08:41:13

看来这可能是 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?

沫离伤花 2024-12-26 08:41:13

这对我有用,难道你的模型填充有问题吗?

This works for me, could it be that you have something wrong in the filling of your model?

旧梦荧光笔 2024-12-26 08:41:13

由于您在 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 ID

Instead of doing x.ID, I think you just want Model.ID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文