0

我有这个选择,我需要将它转换为 DropDownlistFor 调用。

任何人都可以帮忙:

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

这是一个骨架,我只是不确定如何设置 SelectList 和 Selected Value

@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 
           )
4

1 回答 1

0

DropDownListFor 通过提供源/目标属性作为第一个参数,将选项数组作为第二个参数来工作。在您的示例中:

questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID

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

于 2017-02-21T17:02:44.710 回答