Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownListFor does not select value if in for loop

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.

like image 264
Eyeslandic Avatar asked Apr 13 '10 16:04

Eyeslandic


3 Answers

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.

like image 85
Nick Avatar answered Nov 11 '22 03:11

Nick


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 :)

like image 40
Neil Billingham Avatar answered Nov 11 '22 02:11

Neil Billingham


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.

like image 9
Kevin Dark Avatar answered Nov 11 '22 03:11

Kevin Dark