所以如果设置一个 DropDownList where Text: "people names" (string)
和值(value):studentID (int)
.在我看来是这样的(假设 myDDL 是来自放置在 viewbag 中的代码隐藏数据)
@Html.DropDownList("myDDL", (IEnumerable<SelectListItem>)ViewBag.myDDL,
"Select Stuff", new Dictionary<string,object>{
{"class","dropdowns"},{"id","myDDL"}})
“Select Stuff”是出现在下拉列表开头的可选标签,它的值是多少?
我想要这个值,因为如果没有选择任何值,那么我想获取所有数据。
谢谢!
最佳答案
它是空的,它始终是下拉列表的第一个选项,因此请确保将其绑定(bind)到回发操作中的不可为空的属性:
<select name="selectedValue">
<option value="">Select Stuff</option>
...
</select>
你似乎也在使用
myDDL
作为错误的下拉列表助手的第一个参数和第二个参数。第一个参数是用于获取选定值的属性。第二个是可用值,必须是 IEnumerable<SelectListItem>
.所以这样的事情会更有意义:
@Html.DropDownList(
"selctedValue",
(IEnumerable<SelectListItem>)ViewBag.myDDL,
"Select Stuff",
new { @class = "dropdowns", id = "myDDL" }
)
但真正有意义的,我建议你摆脱这个
ViewBag
并使用 View 模型和此助手的强类型版本:@model MyViewModel
...
@Html.DropDownListFor(
x => x.SelectedValue,
Model.MyDdlItems,
"Select Stuff",
new { @class = "dropdowns", id = "myDDL" }
)
关于asp.net-mvc-3 - HTML DropDownList 的 "optionLabel"的默认值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902982/