如何将 JavaScript 数组发送到 .net core 3.1 应用程序中的控制器?
How do I send a JavaScript array to a controller in a .net core 3.1 application?
事情的 javascript 方面看起来像这样:
var self = this;
self.services = ko.observableArray([]);
self.saveServices = function () {
if (self.services().length > 0) {
var obj = JSON.stringify({ services: self.services() });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: "POST",
url: '/Business/SaveServices',
data: obj,
success: function (data) {
$("#saveModal").modal('show');
if (!data) {
$("#saveDetails").text('Processing error, please try again or email us.');
return;
} else if (data === "saved") {
$("#saveDetails").text('Saved changes.');
setTimeout(function () {
$('#saveModal').modal('hide');
}, 2500);
} else {
$("#saveDetails").text(data);
}
},
error: function (error) {
$("#saveModal").modal('show');
$("#saveDetails").text('Processing error, please try again or email us.');
}
});
}
}
然后我的控制器看起来像这样:
[HttpPost]
public async Task<JsonResult> SaveServices([FromBody]SaveServicesRequest saveServicesRequest)
{
try
{
if (User.Identity.IsAuthenticated)
{
return Json("saved");
}
return Json("User is not authenticated.");
}
catch (Exception ex)
{
logger.Error(ex + ". Users Email address: " + User.Identity.Name);
return Json("Processing error, please try again or email us.");
}
}
public class SaveServicesRequest
{
public List<services> services { get; } = new List<services>();
}
public class services
{
public string service { get; set; }
}
我正在访问控制器,但没有返回我期望的项目数组(saveServicesRequest 为空),我似乎无法以任何形式传递数组。有人看到做错了什么吗? data/json 正在客户端发送,认为问题可能出在如何构建 json 和接收它。
问题是您正在将 services
初始化为 SaveServicesRequest
中的新列表。这些属性还应该有 public getters and setters 用于模型绑定。
尝试将您的 class 更新为:
public class SaveServicesRequest
{
public List<services> services { get; set; }
}
事情的 javascript 方面看起来像这样:
var self = this;
self.services = ko.observableArray([]);
self.saveServices = function () {
if (self.services().length > 0) {
var obj = JSON.stringify({ services: self.services() });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: "POST",
url: '/Business/SaveServices',
data: obj,
success: function (data) {
$("#saveModal").modal('show');
if (!data) {
$("#saveDetails").text('Processing error, please try again or email us.');
return;
} else if (data === "saved") {
$("#saveDetails").text('Saved changes.');
setTimeout(function () {
$('#saveModal').modal('hide');
}, 2500);
} else {
$("#saveDetails").text(data);
}
},
error: function (error) {
$("#saveModal").modal('show');
$("#saveDetails").text('Processing error, please try again or email us.');
}
});
}
}
然后我的控制器看起来像这样:
[HttpPost]
public async Task<JsonResult> SaveServices([FromBody]SaveServicesRequest saveServicesRequest)
{
try
{
if (User.Identity.IsAuthenticated)
{
return Json("saved");
}
return Json("User is not authenticated.");
}
catch (Exception ex)
{
logger.Error(ex + ". Users Email address: " + User.Identity.Name);
return Json("Processing error, please try again or email us.");
}
}
public class SaveServicesRequest
{
public List<services> services { get; } = new List<services>();
}
public class services
{
public string service { get; set; }
}
我正在访问控制器,但没有返回我期望的项目数组(saveServicesRequest 为空),我似乎无法以任何形式传递数组。有人看到做错了什么吗? data/json 正在客户端发送,认为问题可能出在如何构建 json 和接收它。
问题是您正在将 services
初始化为 SaveServicesRequest
中的新列表。这些属性还应该有 public getters and setters 用于模型绑定。
尝试将您的 class 更新为:
public class SaveServicesRequest
{
public List<services> services { get; set; }
}