DropDownListFor does not select value if in for loop

后端 未结 6 1161
你的背包
你的背包 2020-12-05 17:49

In my view

<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>

in my controller

public int[ ] C         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 18:31

    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.

提交回复
热议问题