DropDownListFor does not select value if in for loop
Solution 1
I'm getting the same too. When using foreach to loop around a DropDownListFor (i.e. to render multiple select elements on a page).
My work around is to set the selected value in the controller rather than the view: something like this:
In the controller:
public class FruitList
{
public int? selectedFruit{ get; set; }
public List<SelectListItem> fruits
{
get
{
fruitEntities F = new fruitEntities();
List<SelectListItem> list = (from o in F.Options
select new SelectListItem
{
Value = o.fruitID,
Text = o.fruit,
Selected = o.fruitID == selectedFruit
}).ToList();
return list;
}
}
}
public class ViewModel
{
public List<FruitList> collectionOfFruitLists { get; set; }
}
In the view
<table>
<% for (int i=0; i < Model.collectionOfFruitLists.Count; i++ )
{ %>
<tr>
<td><%: Html.DropDownList("fruitSelectList", collectionOfFruitLists[i].fruits, "Please select...") %></td>
</tr>
<%} %>
</table>
The nifty bit is Selected = o.fruitID == selectedFruit
in the controller which acts like a SQL CASE statement; this is really well explained by Lance Fisher (thanks Lance, your post really helped me out :)
Solution 2
Not sure if this is new to mv4 or if it exists in prior version. But the DropDownListFor includes an additional parameter for the SelectList Constructor.
SelectList(IEnumerable, String, String, Object)
For example:
Html.DropDownListFor( x => x.Countries[ i ], New SelectList(Model.CountryList,"ID","Description",Model.Countries[i]))
Where ID
is the Country ID in the CountryList
object and Description
is the Country Name.
Solution 3
I know this question is a bit old but I just came across this problem with looping through a list of objects and attempting to bind the values to DropDownListFor(s) in my Edit View.
I overcame the issue with an inline solution by using the logic from some of the previous solutions given by others for this question.
Binding to my Model:
@Html.DropDownListFor(model => model.QuestionActions[i].QuestionActionTypeId,
Model.QuestionActionTypes.Select(x => new SelectListItem() { Value = x.Value, Text = x.Text, Selected = (x.Value == Model.QuestionActions[i].QuestionActionTypeId.ToString()) }).ToList(),
"Select Action Type",
new { })
Model.QuestionActionTypes is a SelectList that is populated in my Controller.
Eyeslandic
Actively looking for remote jobs. Ruby on Rails, Angular 2+, Postgres Profiles https://gitlab.com/arni1981 https://arni1981.github.io https://www.linkedin.com/in/arni1981/
Updated on June 07, 2022Comments
-
Eyeslandic almost 2 years
In my view
<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>
in my controller
public int[ ] Countries { get; set; } public List<SelectListItem> CountryList { get; set; }
When the forms gets posted there is no problem, the dropdown is populated and the values the user selects are posted. But when I try to load the form with already assigned values to the Countries[ ] it does not get selected.
-
Eyeslandic about 14 yearsThe "i" is not the problem I think, because when I post the form the values from the dropdowns are posted correctly into the Countries[] property.
-
enashnash almost 13 yearsYou don't need the
? true : false
bit, justSelected = o.fruitID == selectedFruit
will do. -
Chris Holmes over 12 yearsMr B - good solution man. I get a StackOverflow error when I run your code, but the idea is sound - you have to create the SelectList for each dropdown and explicitly set the SelectedItem in the Controller. Nice work. This was driving me nuts!
-
iGanja about 11 years+1 for getting it to work for me. I still don't want to recreate my SelectList for every element in my loop though, so I'm gonna keep searching.
-
iGanja about 11 years"i" is necessary for those of us who are posting back the results, and need the collection to bind on the server.
-
Krishna Gupta over 8 yearsThanks! Worked well for me, to display a dropdown (Location Id) in each row of a table.
@Html.DropDownListFor(x=>x.OrderLineItems[i].OrderMfgLocationId, new SelectList(Model.MfgLocations, "Value", "Text", Model.OrderLineItems[i].OrderMfgLocationId))
-
user627283 about 8 yearsJust want to say that as of today (2016ish) with MVC5, this is still an issue... This is one of those "But why??" that get lost in the universe, when logically you'd think it should work but just doesn't for no apparent reason. You'd think Microsoft would say a "By the way", but just doesn't either.. Thank you stackoverflow!! I will try to resolve myself to do it this way... ughhh
-
nothingisnecessary about 7 yearsdoesnt select the matching item in list - adds new item to top of list. usable, but not ideal.
-
Wildcat Matt almost 3 yearsClever! I spent hours trying to find a fix like this.