C# 将数字数组从JS发送到asp.net MVC操作
我想将数字数组发送到我的back操作,无论我尝试什么,我都会得到NULL或error。这是我当前的代码 JSC# 将数字数组从JS发送到asp.net MVC操作,c#,jquery,asp.net,http-post,C#,Jquery,Asp.net,Http Post,我想将数字数组发送到我的back操作,无论我尝试什么,我都会得到NULL或error。这是我当前的代码 JS $.ajax({ traditional: true, dataType: "json", contentType: 'application/json; charset=utf-8', type: "POST", data: JSON.stringify(groupIds), url: '/
$.ajax({
traditional: true,
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: "POST",
data: JSON.stringify(groupIds),
url: '/Admin/ReadMessages',
error: function (error) {
swal.fire({
title: "Something went wrong. Please try again later.",
type: "error"
});
}
});
MVC
public ActionResult ReadMessages(List<long> groupIds)
{
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
public ActionResult ReadMessages(列出组ID)
{
返回新的HttpStatusCodeResult(HttpStatusCode.OK);
}
有什么想法吗?试试这个
var arr= new Array();
arr.push(1);
arr.push(2);
$.ajax({
type: "POST",
url: urlToPost,
data: JSON.stringify(arr),
contentType: "application/json"
});
在控制器中:
public ActionResult ReadMessages(IEnumerable<long> groupIds))
{
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
public ActionResult ReadMessages(IEnumerable groupId))
{
返回新的HttpStatusCodeResult(HttpStatusCode.OK);
}
只是为了给其他人澄清,正如@Amin所说的,我意识到,错误在于发送非数组对象,我使用了map函数,它返回了一些jquery数组,但当我实例化新数组并发送它时,它工作得非常好
$.ajax({
traditional: true,
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: "POST",
data:{groupIds:groupIds},
url:'@Url.Action("ReadMessages", "Admin")',
error: function (error) {
swal.fire({
title: "Something went wrong. Please try again later.",
type: "error"
});
}
});
控制器:
[HttpPost]
public ActionResult ReadMessages(IEnumerable<long> groupIds))
{
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
[HttpPost]
公共操作结果读取消息(IEnumerable GroupId))
{
返回新的HttpStatusCodeResult(HttpStatusCode.OK);
}
U帮助我意识到我做错了什么,错误不在帖子中,而是在我的数组中。。。Ty@Serlok我确实考虑过了,所以我决定把数组放在这里。所以我很乐意帮助你;)
[HttpPost]
public ActionResult ReadMessages(IEnumerable<long> groupIds))
{
return new HttpStatusCodeResult(HttpStatusCode.OK);
}