设置Html.DropdownList的默认值和CSS类

时间:2012-06-12 13:53:25

标签: asp.net-mvc-3 drop-down-menu

我在我的控制器中:

   ViewBag.CityId = new SelectList(new CityRepository().GetAll().OrderBy(x => x.Name),"CityId", "Name", new Guid("74898AD1-73BB-41E1-B1E1-484EACAFA4C4"));

在我的观点中:

<%:Html.DropDownList("CityId", (IEnumerable<SelectListItem>)ViewBag.CityId, new { @class = "select-choice-1" })%>

但问题是没有选择默认值。我如何一次设置默认值和Css类?

1 个答案:

答案 0 :(得分:2)

您需要为下拉列表使用不同的构造函数:

@Html.DropDownListFor(model => model.SelectedCity, (IEnumerable<SelectListItem>)ViewBag.CityId, "Select a City", new { @class = "select-choice-1" })

进一步阅读:MVC 3 Layout Page, Razor Template, and DropdownList