5

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?

2 Answers 2

6

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to set this value in the view instead?
It is, by why would you want to? It's bad practice.
Hi @rudeovski ze bear, It's works for me. thank you very much.
1

Just do:

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

1 Comment

May be he meant "using javacript"... but lets wait for him to update the question or something.

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.