2017-02-17 49 views
0

我有这个选择,我需要将其转换为DropDownlistFor调用。转换选择DropDownListFor MVC 5

谁能帮帮忙:

<select class="form-control" name="QuestionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID" id="@selectId"> 
    @foreach (var x in questionBasicSection.Questions[j].LookupGroup) 
    { 
     <option value="@x.Value" selected="@x.Selected">@x.Text</option> 
    } 
</select> 

这里是一个骨架,我只是不知道如何设置选择列表的,选择的值

@Html.DropDownListFor(q => questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID, 
      new SelectList(
        new List<Object>{ 
         new { value = 0 , text = "Red" }, 
         new { value = 1 , text = "Blue" }, 
         new { value = 2 , text = "Green"} 
        }, 
        "value", 
        "text", 
        // Not sure how to set selected 
      ) 

回答

0

DropDownListFor通过提供电源正常/目标属性作为第一个参数,选项数组作为第二个参数。在您的例子:

questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID

那你的模型属性应包含您希望将DropDownList设置为初始值。例如,如果希望DropDownList在页面加载时显示“蓝色”,请在控制器(或创建模型的任何位置)中将值ANSWER_LOOKUP_OPTION_ID设置为2。