Im wondering if anyone can explain the following disparity in functionality between two ASP.Net MVC 3 projects.
In both projects I have a view model which contains the following:
public List<int> Questions;
And in both projects I have the following ListBoxFor code:
@Html.ListBoxFor(x => x.Questions, new MultiSelectList(ViewBag.Questions as List<MyStandardLib.Mvc.Attribute>, "Id", "Name", @Model.Questions), new { @class = "ui-field-multiselect", style = "width: 250px;" })
The difference in functionality is that when run, one project binds the existing selected Questions correctly, the other does not and shows all Questions as unselected. Stepping through the code, the List is populated in the view and is correctly passed to the MultiSelectList constructor, but it isn’t setting the values as selected.
This is really frustrating.
Change your
ViewBag.Questionsvariable name toViewBag.AvailableQuestions. Sometimes the renderer gets confused about what you are referring to.