[英]getting null value in list when passing by ajax call to mvc controller
I am passing my list to an mvc controller but I am getting null value in the controller.But my list has values when show in alert on client side. 我将我的列表传递给mvc控制器,但我在控制器中获取空值。但是当我在客户端显示警报时,我的列表中有值。
ajax call ajax电话
$("#addTiles").click(function() {
userTiles = JSON.stringify({
'userTiles': userTiles
});
alert("Entered function.");
alert(userTiles[0].TileID);
var url = '@Url.Action("AddTiles")';
$.ajax({
type: "GET",
url: url,
data: userTiles,
success: function(d) {
if (d.indexOf('"IsSessionExpired":true') != -1) {
location.reload();
} else {
onAddTilesSuccessful(d);
}
},
error: function() {
errorInOperation();
},
contentType: "application/html; charset=utf-8",
dataType: 'html'
});
});
function onAddTilesSuccessful(e) {
$("#tilesSubmissionMsg").append(e);
}
function errorInOperation(d) {
$("#tilesSubmissionMsg").append("Something went wrong");
}
mvc controller mvc控制器
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
return View();
}
List Model 列表模型
public class UserTilesVM
{
public int TileID { get; set; }
public int TopPosition { get; set; }
public int LeftPosition { get; set; }
}
List in javascript 在javascript中列出
"{"userTiles":[{"TileID":"3","TopPosition":0,"LeftPosition":0}]}"
I have also tried by sending my list with stringfy but that also doesn't work. 我也尝试过用stringfy发送我的列表,但这也行不通。
You have contentType and dataType twice in your AJAX setup, with different values, which will break the AJAX call. 您在AJAX设置中有两次contentType和dataType,具有不同的值,这将打破AJAX调用。
Keep in mind contentType is to tell the server what type of data to expect and dataType is to determine what type of data is returned, from the documentation . 请记住,contentType是告诉服务器期望的数据类型,dataType用于确定从文档中返回的数据类型。
Edit: I see you have edited your code! 编辑:我看到你编辑了你的代码!
In this case, since you are using JSON.Stringify to modify the data you are sending, you would use contentType: "application/json; charset=utf-8",
as your contentType, since you are sending JSON data to the backend. 在这种情况下,由于您使用JSON.Stringify来修改要发送的数据,因此您将使用
contentType: "application/json; charset=utf-8",
作为您的contentType,因为您要将JSON数据发送到后端。
Use : [HttpGet]
on the method AddTiles
as you have used type: "GET"
on the Ajax hit. 使用方法:
[HttpGet]
的方法AddTiles
因为你已经使用type: "GET"
在阿贾克斯打。
[HttpGet]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
return View();
}
If Still doesn't works then try type: "POST"
on Ajax hit and on method use [HttpPost]
如果仍然不起作用,那么尝试
type: "POST"
Ajax命中的type: "POST"
和方法使用[HttpPost]
[HttpPost]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
return View();
}
when we are trying to pass object data using ajax, we have to store data in variable and pass data directly using "data :'variable'" in AJAX to Controller Method 当我们尝试使用ajax传递对象数据时,我们必须将数据存储在变量中并使用AJAX中的“data:'variable'”直接传递数据到Controller方法
$("#addTiles").click(function() {
var userTiles = ({
'userTiles': userTiles
});
alert("Entered function.");
alert(userTiles[0].TileID);
var url = '@Url.Action("AddTiles")';
$.ajax({
type: "POST",
url: url,
data: userTiles,
success: function(d) {
if (d.indexOf('"IsSessionExpired":true') != -1) {
location.reload();
} else {
onAddTilesSuccessful(d);
}
},
error: function() {
errorInOperation();
},
contentType: "application/html; charset=utf-8",
dataType: 'html'
});
});
function onAddTilesSuccessful(e) {
$("#tilesSubmissionMsg").append(e);
}
function errorInOperation(d) {
$("#tilesSubmissionMsg").append("Something went wrong");
}
//Use [HttpPost] keyword for getting value which was passed by AJAX. //使用[HttpPost]关键字获取AJAX传递的值。
[HttpPost]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
return View();
}
I think your list definition is not ok: 我认为你的列表定义不合适:
"{"userTiles":[{"TileID":"3","TopPosition":0,"LeftPosition":0}]}"
should be: 应该:
"{"userTiles":[{"TileID":"3","TopPosition":"0","LeftPosition":"0"}]}"
i have using this sequence that work fine 我使用这个顺序工作正常
you have check the contentType: "application/json", dataType: "json", sequence in ajax method 你检查了contentType:“application / json”,dataType:“json”,ajax方法中的序列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.