[英]Convert Select to DropDownListFor MVC 5
我有這個選擇,我需要將其轉換為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
)
DropDownListFor通過將source / destination屬性作為第一個參數,並將選項數組作為第二個參數來工作。 在您的示例中:
questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID
模型上的該屬性應包含您希望將DropDownList設置為的初始值。 例如,如果希望DropDownList在頁面加載時顯示“藍色”,則在控制器中(或創建模型的任何地方)將ANSWER_LOOKUP_OPTION_ID
的值設置為2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.