Posts in this series:
So far we have created a form which performs a postback to the server to add more input fields to the same page. We have encountered an issue with MVC seeming to cache partial views (or at least the values inside them).
Step 4 attempts to fix the caching of the selected value field.
I spent hours looking for a way to stop MVC caching partial views, which is what I thought was happening. I tried disabling the output cache and loading the partial views via actions, neither of which helped. I finally found this answer on StackOverflow; the ModelState holds the old values when a view is reloaded via an HTTP POST even if you change the values of the model in the action.
By removing the specific value from the ModelState the value clears properly and the view displays as expected.
private ActionResult AddPostback(OrderViewModel model)
model.SelectedMenuItem = null;
return View("Order", model);