4

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?

Bill Sempf
  • 976
  • 2
  • 13
  • 40
  • I removed the `virtual` attributes of the email model, as it was pointed out that they were incorrect. It made no difference in the outcome. – Bill Sempf Dec 02 '11 at 16:33
  • 1
    Bill - This works for me almost as written. There's a problem with EmailAddress having a member named EmailAddress, though: "Member names cannot be the same as their enclosing type". When I changed it to TheEmailAddress, it worked fine. – Robaticus Dec 02 '11 at 18:27
  • 2
    @Robaticus I started a bounty on this as a favor to Bill, before realizing you'd commented. If the issue truly is the naming of the field, would you create an answer to that effect so that you can earn the bounty? – Seth Petry-Johnson Dec 05 '11 at 18:33
  • That's not the problem .. but it IS a problem. When I run this it still doesn't put the child ID in the ID field. I think it is more aobut the IDs being named the same, and less about anything else, but I am not sure. – Bill Sempf Dec 05 '11 at 19:35
  • I have come to the conclusion that this is a bug in Grid somehow. Clearly some people are getting it to work. In the debugger the Model CLEARLY has the right value for EmailAddress.ID, but the grid CLEARLY puts the wrong value in there. For now, I am going to write the grid out of this screen. Future plans incude referencing the MvcContrib source code and running this puppy through a debugger. – Bill Sempf Dec 12 '11 at 15:41

3 Answers3

1

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?

eth0
  • 4,977
  • 3
  • 34
  • 48
  • No, but I will try that and let you know. – Bill Sempf Dec 08 '11 at 13:41
  • I can't change it. It is that way all the way down through the service layer, and if I change the Model Automapper breaks. – Bill Sempf Dec 12 '11 at 14:22
  • @BillSempf: are you aware that you can override the default conventions in Automapper? If you change the model property, you can use Mapper.CreateMap().ForMember(...) to manually specify how specific fields should be bound. – Seth Petry-Johnson Dec 12 '11 at 15:37
  • Suprisingly, no change. The ForMember worked, but the value is still the right value in the debugger but the parent's ID in the generated HTML. GOT to be a bug in the grid. – Bill Sempf Dec 12 '11 at 16:04
  • I am awarding the bounty here because it does seem that there is a bug in the Grid. Thanks everyone for the research! – Bill Sempf Dec 12 '11 at 19:23
  • @BillSempf extremely strange. Are you using the latest MVC Contrib version? You could try creating a new bug over at http://mvccontrib.codeplex.com or posting on the mailing list: http://groups.google.com/group/mvccontrib-discuss – eth0 Dec 13 '11 at 09:04
1

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

JoJa
  • 612
  • 5
  • 8
  • I don't think so. In the debugger, the model has the correct IDs, but the Grid is populating with the parent ID. If I don't use the grid and instead use a foreach loop to manually make a table it works fine. – Bill Sempf Dec 08 '11 at 13:41
1

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

DavidAndroidDev
  • 2,371
  • 3
  • 25
  • 41
  • No, the Grid wprks, and shows me all of the emailaddresses. x.Email works, and is clearly an EmailAddress object. Model.ID gives me the ID of the Market as it should. – Bill Sempf Dec 12 '11 at 14:23
  • @BillSempf Ah, my mistake. I misread that sentence after the grid code. My apologies. – DavidAndroidDev Dec 12 '11 at 15:25