问题描述
我的视图中有很多下拉列表,例如:
I have a bunch of drop down lists in my view, such as this one:
@Html.DropDownListFor(model => model.Process, Model.ProcessList)
它们都包含 SelectList
基于这样的数据库表数据:
They all take in a SelectList
based on data from a database table like this:
ProcessList = new SelectList(_db.Processes, "ID", "Name");
其中 _db.Processes
返回 ObjectSet< Process>
。
我遇到的问题是,有时下拉列表设置的属性可能是没有选择,即 null
。如何将 null
选择添加到 SelectList
>
The issue I have is that sometimes the property that is set by the drop down list can be no selection, i.e. null
. How can I add a null
selection to the SelectList
>
推荐答案
@Html.DropDownListFor(model => model.Process, Model.ProcessList,"--Select Process--")
以上行将添加-选择过程-
在选择列表的顶部,如果选择了此值,则将发布空字符串,并将bound属性设置为null(在这种情况下为Process)
The above line would add --select process--
at the top of select list and if this value is selected, empty string will be posted and bound property will be set to null (Process in this case)
这篇关于具有空选择的SelectList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!