Reputation: 3
One of those situations where you encounter validation errors after submitting the form, and you want to redirect back to the form, but would like the URL, to reflect the URL of the form, not the page action.
If I use the viewData parameter, my POST parameters will be changed to GET parameters.
How to avoid it? I want that option has not been changed to GET parameters.
Upvotes: 0
Views: 234
Reputation: 1038820
The correct design pattern is not to redirect in case of a validation error bu render the same form again. You should redirect only if the operation succeeds.
Example:
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
// some validation error occurred => redisplay the same form so that the user
// can fix his errors
return View(model);
}
// at this stage we know that the model is valid => let's attempt to process it
string errorMessage;
if (!DoSomethingWithTheModel(out errorMessage))
{
// some business transaction failed => redisplay the same view informing
// the user that something went wrong with the processing of his request
ModelState.AddModelError("", errorMessage);
return View(model);
}
// Success => redirect
return RedirectToAction("Success");
}
This pattern allows you to preserve all model values in case some error occurs and you need to redisplay the same view.
Upvotes: 1