Using HTML.Grid to display child object

1.2k Views Asked by At

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?

3

There are 3 best solutions below

6
On BEST ANSWER

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?

1
On

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

2
On

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