【问题标题】:Html.DropDownListFor selected value is not setting as selectedHtml.DropDownListFor 选定值未设置为选定值
【发布时间】:2021-06-30 09:44:58
【问题描述】:

我打印下拉列表的代码是这样的

        @Html.DropDownListFor(n => n.LevelTwo,
        new SelectList(""),"My selected value", new { @class = "custom-select" })

这给了我这样的输出

<select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option value="">My selected value</option>
但我希望它是这样的
    <select class="custom-select"   id="LevelTwo" name="LevelTwo">
<option selected="selected" value="My selected value">My selected value</option>

我怎样才能做到这一点? 注意:下拉列表的其他元素稍后将使用 Ajax 添加。这不会破坏上面解释的用例

【问题讨论】:

    标签: c# asp.net-mvc razor html.dropdownlistfor


    【解决方案1】:

    将您的代码更改为:

    @Html.DropDownListFor(n=> n.LevelTwo, new List<SelectListItem> { new SelectListItem{ Text= "My selected value", Value= "My selected value" }}, new { @class = "custom-select" })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多