I have an MVC form for adding a simple entity. I am using TextBoxFor(model => model.FieldName) to create the input fields. I have a Save button and a Save and New button.
I would suggest using
ModelState.Clear();
instead of
ViewData = null;
since I find that much more clear what you are trying to do. Although both will accomplish what you're trying to do.
The issue here is that your ViewData.ModelState
is still populated with the values from the original post, even if the Model
is null and you don't explicitly pass any values into your view.
I actually don't think redirecting to the original action is that ugly of a solution, but if you don't want to do that then clearing out the ViewData
should work for you:
[HttpPost]
public ActionResult Save(TestModel model)
{
ViewData = null;
return View();
}
I had the same issue see here:
Updating value provider prior to TryUpdateModel
However - after a post - if it is successful, you should by design be redirecting to a GET action for your next data. This is part of the PRG (post-redirect-get) pattern that is meant to be used by mvc. The helpers assume you are using this pattern and if you are displaying information after a post, there mustve been an error so they redisplay the posted values for "correcting". Try not to work around this - but refactor your code to properly work with PRG.