MVC does not use ViewState, which means that you will need to manage the persistence of the value yourself. This is usually done using your model. So, given that you have a view model, for example:
public class MyViewModel { }
And your controller:
public class MyController : Controller { public ActionResult Something() { return View(new MyViewModel()); } public ActionResult Something(MyViewModel model) { if (!ModelState.IsValid) return View(model); return RedirectToAction("Index"); } }
Now, when you pass the model back to the data view (possibly wrong - the validation fails), when you use your DropDownListFor method, just pass the value:
@Model.DropDownListFor(m => m.Whatever, new SelectList(...))
... etc.
Model Binding MVC will take care of reading the data in your model, you just need to make sure that you pass this back to the view in order to display the same value again.
source share