我有这在我的控制器:的SelectList选择的默认值
ViewBag.ClientID = new SelectList(db.Clients, "ID", "Name");
这在我的视图(用剃刀)
@Html.DropDownList("ClientID", null, htmlAttributes: new { @class = "form-control",})
当我加载我视图我有第一客户端默认选择和我想要“ - 选择客户端 - ”
我有这在我的控制器:的SelectList选择的默认值
ViewBag.ClientID = new SelectList(db.Clients, "ID", "Name");
这在我的视图(用剃刀)
@Html.DropDownList("ClientID", null, htmlAttributes: new { @class = "form-control",})
当我加载我视图我有第一客户端默认选择和我想要“ - 选择客户端 - ”
您可以使用此overload of Html.DropDownList()添加选项标签:
@Html.DropDownList("ClientID",
null,
"Select Client",
htmlAttributes: new { @class = "form-control"})
,如果你想从数据库中设置的SelectedValue然后将其设置在SelectList()
构造函数@Nadeem汗公布。
你可以用下面的方式做。请看下面提到的答案
ViewBag.ClientID = new SelectList(db.Clients, "ID", "--Select Client--", selectedValue);
让我知道它是否对你有帮助。 感谢
在你看来试试这个: -
@Html.DropDownList("ClientID",null,"--Select Client--", htmlAttributes: new { @class = "form-control",})
您需要使用的DropDownList这个重载版本。
OP明确询问如何在ASP .NET MVC中完成它。告诉他们“在MVC中搜索同样的东西”不是一个答案。他们已经通过搜索来到这里...你可以修改答案,包括一个MVC片段。 – Belogix 2014-11-06 10:48:28
@Belogix:我编辑了我的答案。没有必要downvote。虽然让它 – BNN 2014-11-06 10:49:37
删除downvote,因为你现在至少提供了一些MVC片段,谢谢。 – Belogix 2014-11-06 10:50:30