I'd so appreciate if someone could advise.
I have an Ajax form:
@using (Ajax.BeginForm("Edit", null, new AjaxOptions() {
UpdateTargetId = updateRegion,
InsertionMode = InsertionMode.Replace,
OnFailure = "formFailure" }))
{}
Its UpdateTargetId
differs based on the current User role:
@{
if (curUser.IsInputer)
{
updateRegion = "content";
}
else if (curUser.IsAuthorizer)
{
updateRegion = "mainPane";
}
}
If the modelstate is invalid I'd like to return view in mainPane
always:
<script>
function formFailure(result)
{
$("#mainPane").html(result.responseText);
}
</script>
However onFailure is not called when ModelState is invalid. For this, I set error code in controller:
public ActionResult Edit(ContractModel entity)
{
if(ModelState.IsValid)
{
if(curUser.isInputer) { return RedirectToAction("SomeAction");}
if(curUser.Authorizer) { return RedirectToAction("OtherAction");}
}
Response.StatusCode = 500;//internal server error to fire OnFailure of form
return PartialView(entity);
}
Then I get the desired result, i.e. I see the model with its errors in mainPane
div and internal server error in browser console. However, it works this way when I run the application locally, when I publish and run it on the server, I see the error 500 Internal server error
, instead of the partial view. Is there any workaround?
EDIT: As an option I tried to check Model for errors in form OnSuccess handler:
var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid));
if (!isValid) {
$("#mainPane").html(result.responseText);
}
But I still get the view in "content" div. Why is this happening?