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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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...."
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: stackoverflow.com/questions/390083/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.