简体   繁体   中英

OnFailure/ OnSuccess is not called on ajax form submit

I am trying to submit a ajax form and what i want is to display a confirmation message onsuccess and / or onfailure.

But These two functions never called.

@using (Ajax.BeginForm("Transfer", "Location", null, new AjaxOptions
{
    UpdateTargetId = "update-message",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
    OnSuccess = "updateSuccess",
    OnFailure = "updateFailure"

}, new { @id = "transferForm" }))
{
  // html here
}
<div id="update-message"></div>
<div id="commonMessage"></div>

this is script:

<script>
function updateSuccess() {
    if ($("#update-message").html() == "True") {
        $('#commonMessage').html("The ownership has been transfered.");
        $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
    }
    else {
        $("#update-message").show();
    }
}
function updateFailure() {
    if ($("#update-message").html() == "False") {
        $('#commonMessage').html("The ownership has NOT been transfered.Error     occured.");
        $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
    }
    else {

    }
}
</script>

After the form submission, i can see that it returns True/False and it is displayed in the "update-message" div. But The confirmation never shows up. even if I use an 'alert' inside the onFailure/onSuccess , the alert doesnt show up.

Any help guys??

EDIT: The returned message of controller shows up in the div. But No confirmation message shows up. 在此处输入图片说明

Could you try this:

function updateSuccess() {
        $('#commonMessage').html("The ownership has been transfered.");
        $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
}

function updateFailure() {
    $('#commonMessage').html("The ownership has NOT been transfered.Error     occured.");
    $('#commonMessage').delay(400).slideDown(400).delay(4000).slideUp(400);
}

Because div updated after ajax operations.

NOTE : Does not "OnSuccess" mean "action return true" ? Also, "OnFailure" = "action return false" ? Why do you use "if statement"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM