asp.net-mvcasp.net-mvc-3mvccontribmvccontrib-grid

Using HTML.Grid to display child object


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?


Solution

  • 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?