Ajax.BeginForm with OnComplete always updates page

北战南征 提交于 2019-11-29 18:38:49

问题


I have simple ajax form in MVC. In AjaxOptions there is OnComplete set to simple javascript function which does one thing - returns false.

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { UpdateTargetId = "DivFormId", HttpMethod = "Post", OnComplete = "preventUpdate" }))

function preventUpdate(xhr) {
    return false;       
}

The problem is, that page is already updated. E.g. in one case controller returns partial view after postback, in other case it returns some Json object. I want it to update page when partial view is returned, and to show dialog window when json is returned. Unfortunately when json is returned, it clears the page (update it with json) even when OnComplete function returns false as MSDN says: To cancel the page update, return false from the JavaScript function.

How to prevent page update depending on received response?

Thank you!

----- UPDATE -------

So far I found following solution. When I don't specify UpdateTargetId, I can do manually with the response what I want. But it is still not documented behaviour with return false.


回答1:


Use OnSuccess and get rid of UpdateTargetId. Like this:

@using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "Post", OnSuccess = "foo" }))
{
    ...
}

and then:

function foo(result) {
    if (result.SomePropertyThatExistsInYourJsonObject) {
        // the server returned a JSON object => show the dialog window here
    } else {
        // the server returned a partial view => update the DOM:
        $('#DivFormId').html(result);
    }
}


来源:https://stackoverflow.com/questions/12562857/ajax-beginform-with-oncomplete-always-updates-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!