SelectList选择的默认值 [英] SelectList selected default value
本文介绍了SelectList选择的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的控制器中有这个
ViewBag.ClientID = new SelectList(db.Clients, "ID", "Name");
在我看来(使用剃须刀)
This in my View (using razor)
@Html.DropDownList("ClientID", null, htmlAttributes: new { @class = "form-control",})
加载视图时,默认情况下我选择了第一个客户端,而我想使用 --Select Client-
When I load my view I have the first client selected by default and I want to have "--Select Client--"
推荐答案
,您可以使用此 Html.DropDownList()的重载以添加选项标签:
you can use this overload of Html.DropDownList() to add option label:
@Html.DropDownList("ClientID",
null,
"Select Client",
htmlAttributes: new { @class = "form-control"})
如果要从数据库设置SelectedValue,则在 SelectList()
构造函数中进行设置正如@Nadeem Khan发布的。
if you want to set SelectedValue from database then set it in SelectList()
constructor as @Nadeem Khan posted.
这篇关于SelectList选择的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文