On the create.cshtml file I need to have a default value selected. I got the default value to work but now it won't validate. I googled and someone said that I must use DropDownListFor instead of DropDownList. But I can't get the DropDownListFor to work. Any ideas? Thanks
--controller--
var SelectedState = "New York";
Output
var stateList = new SelectList(db.states, "Name", "Name", SelectedState);
ViewBag.StateOptions = stateList;
--Create.cshtml --
@Html.LabelFor(model => model.State)
@Html.DropDownList("StateOptions", String.Empty) <=== This works but won't validate.
//@@Html.DropDownListFor(model => model.State,new SelectList(ViewBag.StateOptions,"Id","Name"), "Pick State") <== Tried this too but no luck. @
@Html.ValidationMessageFor(model => model.State)
Output
ceci belaPosted Nov 4, 2014, 9:44 AM
BUT this modified version works...meaning it validates with one minor problem: the SelectedState doesn't show up anymore.
@Html.DropDownListFor(model => model.State, (IEnumerable
How can I get the default value on the code above?
Anupam SinghPosted Nov 3, 2014, 11:10 PM
@Html.DropDownListFor(model => model.State, ViewBag.StateOptions as IEnumerable