When model is not valid, return to partial view inside a view, with error message
From what I can see you are using a standard Html.BeginForm
POSTing to the ChangeEvaluator
controller action which either performs a redirect or returns a partial view if validation fails.
So the behavior you are observing is perfectly normal. You will have to submit this form using AJAX if you want to achieve that:
@using (Ajax.BeginForm("ChangeEvaluator", "Ontwikkelplan", new AjaxOptions { OnSuccess = "handleSuccess" })){ ...}
and then you could adapt your controller action so that in case of success it doesn't redirect but it returns a Json object containing the url to redirect to:
[HttpPost]public ActionResult ChangeEvaluator(EvaluatorWijzigenOPViewModel ewopvm){ if (ModelState.IsValid) { ... return Json(new { redirectTo = Url.Action("Evaluatorenlijst") }); } return PartialView("EvaluatorWijzigenPartial", ewopvm);}
and finally write the handleSuccess
javascript function:
function handleSuccess(result) { if (result.redirectTo) { // The controller action returned a JSON object with the redirectTo property // let's redirect to this location: window.location.href = result.redirectTo; } else { // The controller action returned a partial view with the form and the errors // So we need to update some containing DIV with it: $('#someDivThatCOntainsYourForm').html(result); }}