簡體   English   中英

將自定義成功/錯誤消息傳遞給來自ASP.NET控制器的Ajax調用

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

我正在使用ASP.Net Core,並且正在嘗試將所有內容本地化。 我沒有從控制器或視圖中本地化字符串的問題,但是我確實有一個來自javascript的ajax調用,該javascript當前具有硬編碼的英語成功/錯誤消息。 我最初的想法是,我可以將本地化的成功/錯誤消息傳遞回控制器的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函數?

我最終接受了@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);
            }
       });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM