Monday, 9 September 2013

Persist SelectList in model on Post

Persist SelectList in model on Post

In MVC4:
I have the following property in my model used for a dropdown list:
public SelectList Subjects { get; set; }
I set the Subjects property in my Index() Action on page load and return
the model.
The dropdown gets populated just fine with the SelectListItems.
@Html.DropDownListFor(x => x.Subject, new SelectList(Model.Subjects,
"Text", "Text", "Other"))
When I submit the form the Subjects SelectList in the model has changed to
null. There has to be a simple way to persist this on HttpPost. I assume I
want to submit and post this SelectList as well, along with all the form
fields? How would I do this?

No comments:

Post a Comment