首页 > 解决方案 > 从 asp.net 控制器将自定义成功/错误消息传递给 ajax 调用

问题描述

我正在使用 ASP.Net Core,我正在尝试将所有内容本地化。我没有从控制器或视图中本地化字符串的问题,但我确实有来自 javascript 的 ajax 调用,该调用当前具有硬编码的英语成功/错误消息。我最初的想法是我可以将本地化的成功/错误消息从控制器传递回 ajax 调用。我认为使用 ajax 成功函数会很容易,但我不确定如何将错误消息传递给错误/失败函数。

这是我的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);
 }
});

这是我的控制器的功能:

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;
        }           
    }

有没有办法从 SaveGridState 函数定义我的成功/错误消息并将其传递给 ajax 函数?

标签: c#ajaxasp.net-core

解决方案


我最终接受了@MarkG 和@AndreasHassing 的建议并更改了我的函数以返回一个IActionResult,然后使用了Ok() 和BadRequest()。

这是我的控制器功能现在的样子:

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

我的 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);
            }
       });

推荐阅读