2

I have in my controller:

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

And in My View:

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

But the problem is that default value wasn't selected.How can I set default value and Css Class at one time?

Jesse
  • 8,223
  • 6
  • 49
  • 81
Shayan
  • 402
  • 1
  • 4
  • 18

1 Answers1

2

You need to use a different constructor for the drop down list:

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

Further reading: MVC 3 Layout Page, Razor Template, and DropdownList

Community
  • 1
  • 1
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • If you look at my controller code , I want to set dropdowlist selected value to value "74898AD1-73BB-41E1-B1E1-484EACAFA4C4" ,not to "Please select a city...." – Shayan Jun 12 '12 at 15:16
  • You need to review the controller constructor I posted. "Select a City" is simply the option label. IT IS NOT, the selected value. This is not a unique problem you are having, here is another example on what to do to fix your issue: http://stackoverflow.com/questions/390083/asp-net-mvc-html-dropdownlist-value-not-set-via-viewdata-model – Jesse Jun 12 '12 at 15:26