Dropdown list of MVC4 links in list (error)

I have below code in view. (index.cshtml)

Binding problem

  • The first snapshot picks the correct value based on the ChildItem editor template.
  • The second example using inline dropdown does not work. I dont want to create an editor template to display dropdown values.
  • It's strange that the TextBoxFor will show the correct value. This seems to be an issue with the dropdown only.

How do I fix the binding so that the second one goes down? I debugged it. It looks like ViewData.Eval is not picking up the correct value from _.Children[i].ChooseId

.

Update (bug)
This is a confirmed bug (low priority, how?) In the MVC framework http://aspnet.codeplex.com/workitem/8311

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Children.Count(); i++)
    {
       <p>A: @Html.EditorFor(_ => _.Children[i], "ChildItem")</p>
       <p>B: @Html.DropDownListFor(_ => _.Children[i].ChooseId, TestModel.PeopleSelect)</p>
    }
    <button type="submit">GO</button>
}

      

I tried using DropDownListFor(_ => Model.Children[i].ChooseId)

, same result.
using TextBoxFor(_ => _.Children[i].ChooseId)

shows the correct value, wierd?

For reference: ChildItem.cshtml

@using dropdown.Controllers
@using dropdown.Models
@model dropdown.Models.TestPerson
@Html.DropDownListFor(_ => _.ChooseId, TestModel.PeopleSelect)

      

It looks like this:

A works, B does not.

+3


source to share


1 answer


I subsequently found the following: http://aspnet.codeplex.com/workitem/8311 This is a confirmed bug.

The only workaround I've found is this.

Mark selected item



@Html.DropDownListFor(_ => _.Children[i].ChooseId, Mark(TestModel.PeopleSelect, Model.Children[i].ChooseId))

Marking function / extension method

@functions {
    private IEnumerable<SelectListItem> Mark(IEnumerable<SelectListItem> items, object Id)
    {
        foreach (var item in items)
            if (string.CompareOrdinal(item.Value, Convert.ToString(Id)) == 0)
                item.Selected = true;
        return items;
    }
}

      

+2


source







All Articles