【问题标题】:Ajax Action Link Call Back Fails to FireAjax 操作链接回调无法触发
【发布时间】:2015-08-11 19:33:44
【问题描述】:

我有一个带有以下 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 函数来更新我的 &lt;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 似乎从未被使用过,我想知道原因。我做错了什么,我该如何解决?

感谢您的宝贵时间。

【问题讨论】:

  • onSuccess 不属于AjaxOptions 而不属于 HtmlAttributes 吗?
  • 天啊。这是漫长的一天...非常感谢您的宝贵时间。
  • 虽然这是朝着正确方向迈出的一大步,但我仍然没有让我的&lt;span&gt;s 更新?

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


【解决方案1】:

终于搞定了……

我需要通过 NuGet 安装 jQuery unobtrusive ajax。我使用以下包管理器命令完成此操作

PM> 安装包 Microsoft.jQuery.Unobtrusive.Ajax

我希望这可以节省其他人一些时间。


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多