Html.DropDownListFor 所选值未设置为所选

Html.DropDownListFor selected value is not setting as selected

我打印下拉菜单的代码是这样的

        @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 添加下拉列表的其他元素。这不会破坏上面解释的用例

将您的代码更改为:

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