我在我的控制器中:
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类?
答案 0 :(得分:2)
您需要为下拉列表使用不同的构造函数:
@Html.DropDownListFor(model => model.SelectedCity, (IEnumerable<SelectListItem>)ViewBag.CityId, "Select a City", new { @class = "select-choice-1" })