简体   繁体   English

在ajax表单提交中未调用OnFailure / OnSuccess

[英]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. 我正在尝试提交一个ajax表单,我想要的是在成功和/或失败时显示一条确认消息。

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. 提交表单后,我可以看到它返回True / False,并显示在“ update-message” div中。 But The confirmation never shows up. 但是确认从不显示。 even if I use an 'alert' inside the onFailure/onSuccess , the alert doesnt show up. 即使我在onFailure / onSuccess中使用了“警报”,也不会显示警报。

Any help guys?? 有帮助吗?

EDIT: The returned message of controller shows up in the div. 编辑:控制器返回的消息显示在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. 因为div在ajax操作之后更新。

NOTE : Does not "OnSuccess" mean "action return true" ? 注意 :“ OnSuccess”是否表示“操作返回true”? Also, "OnFailure" = "action return false" ? 另外,“ OnFailure” =“操作返回false”吗? Why do you use "if statement"? 为什么使用“ if语句”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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