Using MVC3 I have found that setting a SelectList's selected value correctly renders the view on an HttpGet, but fails to render correctly on HttpPost. I have inspected the Model before they are forwarded to the View on HttpPost and they are correctly being updated, it just seems the View is not rendering the selected tag correctly.
On HttpPost, the <select>
is rendered exactly as it existed after any edits but before submission of the form. The m.SelectedWidgetId = 2;
in the HttpPost method is executed, updates the model, but is not reflected in the View.
What am I missing here?
Model:
public class WidgetModel
{
private Widget[] Widgets {
get
{
return new Widget[] {
new Widget { Id=1, Name="Item 1" },
new Widget { Id=2, Name="Item 2" },
new Widget { Id=3, Name="Item 3" }
};
}
}
public SelectList WidgetList
{
get
{
return new SelectList(Widgets.ToList(), "Id", "Name", SelectedWidgetId);
}
}
public int SelectedWidgetId { get; set; }
}
View:
@model thisProject.Models.WidgetModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.SelectedWidgetId, Model.WidgetList, "Select...");
<input type='submit' />
}
Controller Methods;
public ActionResult Widget()
{
var m = new WidgetModel();
m.SelectedWidgetId = 1;
return View(m);
}
[HttpPost]
public ActionResult Widget(WidgetModel m)
{
m.SelectedWidgetId = 2;
return View(m);
}