2

我的控制器中有:

   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 Class?

4

1 回答 1

2

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

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

进一步阅读:MVC 3 布局页面、Razor 模板和 DropdownList

于 2012-06-12T15:05:18.113 回答