c# - 从 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 函数?
解决方案
我最终接受了@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);
}
});
推荐阅读
- javascript - 引用者未在 GET 请求中发送,但在 Ionic/Capacitor 应用程序的 POST 中发送
- c++ - 为什么静态成员和静态常量成员在初始化时不一样?
- python - 未来在 python 中的使用
- node.js - 如何重新启动 Node.js 应用程序并将新进程移交给控制台
- c# - Azure Functions 的每个环境配置?
- 3d - rgl:如何避免透明 3D 椭球中的莫尔效应?
- python - 如何在任何其他 IDE(如 vs_code)上打开 jupyter 项目
- python - 如何从 Python 对象中提取内存地址
- javascript - 为 youtube 视频添加华丽的弹出窗口
- python - CMAKE 找不到 PythonLibs(缺少:PYTHON_INCLUDE_DIRS)