简体   繁体   English

将自定义成功/错误消息传递给来自ASP.NET控制器的Ajax调用

[英]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. 我正在使用ASP.Net Core,并且正在尝试将所有内容本地化。 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. 我没有从控制器或视图中本地化字符串的问题,但是我确实有一个来自javascript的ajax调用,该javascript当前具有硬编码的英语成功/错误消息。 My initial thought is I could just pass the localized success/error message back to the ajax call from the controller. 我最初的想法是,我可以将本地化的成功/错误消息传递回控制器的ajax调用。 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. 我认为使用ajax成功函数将很容易,但是我不太确定如何将错误消息传递给错误/失败函数。

Here is my ajax call: 这是我的ajax电话:

$.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? 有没有一种方法可以通过SaveGridState函数定义成功/错误消息并将其传递给ajax函数?

I ended up taking the advice of @MarkG and @AndreasHassing and changed my function to return an IActionResult and then used Ok() and BadRequest(). 我最终接受了@MarkG和@AndreasHassing的建议,并更改了函数以返回IActionResult,然后使用了Ok()和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调用看起来像这样:

$.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);
            }
       });

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

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