【问题标题】:Convert Select to DropDownListFor MVC 5将 Select 转换为 DropDownListFor MVC 5
【发布时间】:2017-02-22 01:02:44
【问题描述】:

我有这个选择,我需要将它转换为 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 
           )

【问题讨论】:

    标签: asp.net-mvc razor razor-declarative-helpers


    【解决方案1】:

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

    questionBasicSection.Questions[@counter].ANSWER_LOOKUP_OPTION_ID

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多