Using HTML.Grid to display a child object

I have an ASP.NET MVC application that uses a single view to display the properties and child elements (with their properties) of a model object.

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; } } 

In the view, I want to use a table to display a list of related email addresses. For this I use 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 identifier is the identifier of the parent Market object, not the EmailAddress address.

How can i fix this?

+4
source share
3 answers

This seems to be a bug in WebGrid. We tried to rename my identifier field in the EmailAddress class, for example. EmailID and pass this to WebGrid and see if it displays correctly?

+1
source

Does this work for me, maybe you have something wrong with filling in your model?

+1
source

Since you are using the lambda expression for the Column.For() method, the x parameter refers to a single email. I think you mean a model link, not a single email id

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

+1
source

Source: https://habr.com/ru/post/1383958/


All Articles