Well, I have a simple ajax form:
This is MyPartialView
@using(Ajax.BeginForm("action", "controller", new AjaxOptions { OnBegin = "beginRequest", OnComplete = "completeRequest", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "div-to-replace" }, })) { <input type="text" id="my-input" /> ... }
This is the parent view:
<div id="div-to-replace"> @Html.RenderPartial("MyPartialView") </div>
In my controller, I:
[HttpPost] public ActionResult action(Model model) { if (ModelState.IsValid) { // do staff with model // return partial view return PartialView("MyPartialView"); } // else add error and return json result return Json(new {error = "invalid data"}); }
And my javascript for the full ajax method:
function completeRequest(data) { var result = $.parseJSON(data.responseText); if (result != 'undefined' && result != null && result.error) {
But the problem is that I am empty #div-to-replace if the state of the model is invalid. If the state of the model is fine, everything works fine. If I use a different insert mode, it duplicates the contents of the div before or after the div.
Summary:
I want different InsertionMode behavior depending on json result. I do not need to replace the if (result != 'undefined' && result != null && result.error) data if (result != 'undefined' && result != null && result.error) .
source share