如何使用 asp.net mvc Html.DropDownList 帮助器将自定义项添加到下拉列表的顶部?

Posted

技术标签:

【中文标题】如何使用 asp.net mvc Html.DropDownList 帮助器将自定义项添加到下拉列表的顶部?【英文标题】:How do I add a custom item to the top of a dropdown using the asp.net mvc Html.DropDownList helper? 【发布时间】:2010-10-07 22:55:18 【问题描述】:

我的 asp.net mvc 站点上有一个下拉列表:

<%= html.DropDownList("CustCodes") %>

我的控制器:

 ICallService sc = new CallService();
 IList<CustCode> custCodes = sc.GetCustCodes();
 ViewData["CustCodes"] = new SelectList(custCodes, "ID", "Name");

我想要的是,一旦下拉列表显示在顶部有一个空白条目或显示“选择客户”的选项。我是否需要使用 jquery 来执行此操作,或者 DropDowList 帮助程序中是否有一些我缺少的东西?

谢谢。

【问题讨论】:

【参考方案1】:

使用包含选项标签的覆盖。

 <%= Html.DropDownList("CustCodes",
                       ViewData["CustCodes"] as SelectList,
                       "Select a Customer",
                       null ) %>

【讨论】:

以上是关于如何使用 asp.net mvc Html.DropDownList 帮助器将自定义项添加到下拉列表的顶部?的主要内容,如果未能解决你的问题,请参考以下文章