Razor 如何添加标签;选择一个代理";并将值设为null作为此dropdownlist的第一个选项?

Razor 如何添加标签;选择一个代理";并将值设为null作为此dropdownlist的第一个选项?,razor,model-view-controller,Razor,Model View Controller,代理列表来自一个名为AgentId的视图包,该视图包直接从数据库中读取 @Html.DropDownList("AgentId", null, htmlAttributes: new { @class = "form-control" }) 您可以使用这个重载,它接受选项标签作为第三个参数 Viewbag. AgentId = new Selectlist (db.Agents, "ID", "AgentCentre", distribution.AgentId) 这将添加一个带有文本“选

代理列表来自一个名为AgentId的视图包,该视图包直接从数据库中读取

@Html.DropDownList("AgentId", null, htmlAttributes: new { @class = "form-control" }) 

您可以使用这个重载,它接受选项标签作为第三个参数

Viewbag. AgentId = new Selectlist (db.Agents, "ID", "AgentCentre", distribution.AgentId)
这将添加一个带有文本“选择代理”和无值属性的选项

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList,
    string optionLabel,
    Object htmlAttributes
)
@Html.DropDownList("AgentId", null,"Select an agent", new { @class = "form-control" })