将自定义成功/错误消息传递给来自asp.net控制器的ajax调用

时间:2018-06-18 18:52:31

标签: c# ajax asp.net-core

我正在使用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函数?

1 个答案:

答案 0 :(得分:0)

我最终接受@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);
            }
       });
相关问题