简体   繁体   中英

Passing custom success/error messages to ajax call from asp.net controller

Im working with ASP.Net Core and I'm trying to have everything localized. I have no isses localizing strings from the controller or right in the view but I do have an ajax call from javascript that currently has hard coded english success/error messages. My initial thought is I could just pass the localized success/error message back to the ajax call from the controller. I'm thinking this will be easy enough with the ajax success function but Im not really sure how to pass an error message to the error/fail function.

Here is my ajax call:

$.ajax({
 type: "POST",
 url: '@Url.Action("SaveGridState", "Account", new { gridID = 3 })',
 data: {
   options: JSON.stringify(gridState)
 },
 async: true,
 success: function(data) {
   var staticNotification = $("#staticNotification").data("kendoNotification");

   staticNotification.show({
     message: "Grid State Saved"
   }, "success");

   var container = $(staticNotification.options.appendTo);
   container.scrollTop(container[0].scrollHeight);
 },
 fail: function() {
   var staticNotification = $("#staticNotification").data("kendoNotification");
    staticNotification.show({
     message: "Grid State Save Failed"
    }, "error");

   var container = $(staticNotification.options.appendTo);
   container.scrollTop(container[0].scrollHeight);
 }
});

Here is my function from my controller:

public bool SaveGridState(string options, int gridID)
    {
        try
        {
            UserStore ustore = new UserStore(_settings);
            HttpContext.Session.LoadAsync();
            uint user_id = (uint)HttpContext.Session.GetInt32("UserID");

            options = options.Replace("'", @"\'");

            return ustore.SaveGridState(user_id, gridID, options);
        }
        catch(Exception ex)
        {
            return false;
        }           
    }

Is there a way to define my success/error message from the SaveGridState function and pass it to the ajax function?

I ended up taking the advice of @MarkG and @AndreasHassing and changed my function to return an IActionResult and then used Ok() and BadRequest().

Here is what my controller function looks like now:

public IActionResult SaveGridState(string options, int gridID)
    {
        try
        {
            UserStore ustore = new UserStore(_settings);
            HttpContext.Session.LoadAsync();
            uint user_id = (uint)HttpContext.Session.GetInt32("UserID");

            options = options.Replace("'", @"\'");

            bool success = ustore.SaveGridState(user_id, gridID, options);

            return Ok(new { Result = success, Message = success ? _localizer["GridSaveSuccess"].Value : _localizer["GridSaveFail"].Value });
        }
        catch (Exception ex)
        {
            return BadRequest(new { Result = false, Message = _localizer["GridSaveFail"].Value });
        }
    }

And my ajax call looks like this:

$.ajax({
            type: "POST",
            url: '@Url.Action("SaveGridState", "Account", new { gridID = 3 })',
            data: { options: JSON.stringify(gridState) },
            async: true,
            success: function (data) {
                var staticNotification = $("#staticNotification").data("kendoNotification");

                if (data.Result) {
                    staticNotification.show({
                        message: data.Message
                    }, "success");
                }
                else {
                    staticNotification.show({
                        message: data.Message
                    }, "error");
                }                    

                var container = $(staticNotification.options.appendTo);
                container.scrollTop(container[0].scrollHeight);
            },
            error: function (data) {
                var staticNotification = $("#staticNotification").data("kendoNotification");
                staticNotification.show({
                    message: data.Message
                }, "error");

                var container = $(staticNotification.options.appendTo);
                container.scrollTop(container[0].scrollHeight);
            }
       });

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