I have a .cshtml Razor view with the following div

<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" },
                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>

I then have in my controller some C# code that returns an 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);
    }
}

and I have the following javascript function to update the text in my <span id="notify_failure_message" ....

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

When I click my button my Method fires and it returns the Json object

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

However, it seems my java script function onCompletion is never being used and I want to know why. I have a feeling it is because of onCompletion = "onCompletion", what am I doing wrong and how can I fix it?

Thanks for your time.

Related posts

Recent Viewed