Ajax Action Link回调失败

时间:2015-08-11 19:33:45

标签: c# asp.net html5 asp.net-mvc-4 razor

我有一个带有以下div的.cshtml Razor视图

<div id="software_updates" class="htmlCode tab-pane fade">
    @using (Html.BeginForm(new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
            <div id="update_text_content" class="active fade in">
                <h2>Software Update Notification Service</h2>
                <h4>
                    This function will send all registered users 
                    a notification of a revision/update to the existing software. The message
                    they receive will be the following:
                </h4>
                @{
                    var updateMessage =
                        String.IsNullOrEmpty(Model.ExampleUpdateMessage) ?
                            String.Empty :
                            Model.ExampleUpdateMessage;
                }
                <pre>@updateMessage</pre>
                @Ajax.ActionLink(
                "Send Update Notification",
                "SendUpdateNotifications",
                "Tools",
                null,
                new AjaxOptions 
                { HttpMethod = "GET", OnSuccess = "onSuccess" },
                new
                {
                    onclick = String.Format(
                        "return confirm('Are you sure you want to send a " +
                        "revision notification to the {0:N0} registered {1}?');",
                        @Model.UserList.Count,
                        "user".Pluralize(@Model.UserList.Count)),
                    @class = "btn btn-lg btn-danger",
                    role = "button",
                    onSuccess = "onCompletion"
                })
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <span id="notify_failure_message" class="label label-danger"></span>
                        <span id="notify_success_message" class="label label-success"></span>
                    </div>
                </div>
            </div>
    }
</div>

然后我在我的控制器中有一些C#代码返回ActionResult / JsonResult

[AllowAnonymous]
public async Task<JsonResult> SendUpdateNotifications()
{
    string result = String.Empty;
    try
    {
        ... // Some stuff.
        return Json(new
        {
            notify_success = result.ConvertToHtmlString(),
            notify_failure = String.Empty
        }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception e)
    {
        result = String.Format(
            "DrGroup Revision/Update notifications failed to send\n" + 
            "See the invite Log for more information.\n" + 
            "Error: {0}", 
            e.Message);
        return Json(new
        {
            notify_success = String.Empty,
            notify_failure = result.ConvertToHtmlString()
        }, JsonRequestBehavior.AllowGet);
    }
}

我有以下javascript函数来更新<span id="notify_failure_message" ...中的文字。

@section scripts {
    <script>
        function onSuccess(result) {
            $('#notify_failure_message').html(result.notify_failure);
            $('#notify_success_message').html(result.notify_success);
        }
    </script>
}

当我点击我的按钮时,我的方法会触发,并返回Json对象

return Json(new
{
    notify_success = result.ConvertToHtmlString(),
    notify_failure = String.Empty
}, JsonRequestBehavior.AllowGet);

然而,似乎我的java脚本函数onSuccess从未被使用过,我想知道原因。我做错了什么,我该如何解决?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

终于有了这个......

我需要通过NuGet安装jQuery unobtrusive ajax。我使用以下Package Manager命令

完成了这项工作
  

PM&GT;安装包Microsoft.jQuery.Unobtrusive.Ajax

我希望这能节省一些时间。

请注意,这会阻止其他Ajax通知代码工作。见https://stackoverflow.com/questions/31961761/install-package-microsoft-jquery-unobtrusive-ajax-stopped-download-dialog-popup