Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3: Set Dropdown list Selected Value

I am using mvc3 and I have a drop down list in my view.

@Html.DropDownListFor(m => m.State,
new SelectList(Model.StateList, "Value", "Text"))

Is there a way of setting the selected value in the View?

like image 230
jpo Avatar asked Dec 04 '12 21:12

jpo


1 Answers

Extending on what Romias said, in your controller, set Model.State to whatever value you want. If you wanted 'WI', then Model.State should equal that.

Controller:

public ActionResult Index()
{
    var m = new TestViewModel();
    m.State = "WI";
    return View(m);
}

View:

@Html.DropDownListFor(m => m.State, new SelectList(Model.StateList, "Value", "Text", Model.State))
like image 88
mnsr Avatar answered Sep 18 '22 09:09

mnsr