简体   繁体   English

将选项添加到 razor asp.net mvc 中的下拉列表

[英]Add option to dropdownlist in razor asp.net mvc

I have this dropdownlist:我有这个下拉列表:

@Html.DropDownList("CategoryID", new SelectList(ViewBag.Categories, "Id", "Name"),"Select category", new { @class = "form-select mt-3" })

I want to add a value of null to "Select category", specifically in razor, not in the controller.我想在“选择类别”中添加一个 null 值,特别是在 razor 中,而不是在控制器中。

Any help would be appreciated.任何帮助,将不胜感激。

try it like that:试试这样:

<select asp-for="YourProperty">
<option value="" selected>Select category</option>
@foreach(var item in YourList){
    <option value="@item.Id">@item.YourValue</option>
}
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM