我正在使用ajax调用将多个参数传递给我的mvc控制器,但是我的控制器方法没有被命中。我在复选框检查/取消选中时填充我的列表。如何将多个参数传递给mvc控制器。
AJAX POST
<script>
var userTilesAdd = [];
var userTilesDelete = [];
$("#addTiles").click(function () {
var screenWidth = 900;
//var data = JSON.stringify({ 'userTilesAdd': userTilesAdd }, { 'userTilesDelete': userTilesDelete },{'screenWidth':screenWidth});
var data = JSON.stringify({ 'userTilesAdd': userTilesAdd , 'userTilesDelete': userTilesDelete , 'screenWidth': screenWidth });
alert("Entered function.");
alert(userTiles[0].TileID);
var url = '@Url.Action("AddTiles")';
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: "POST",
url: url,
data: data,
success: function (d) {
if (d.indexOf('"IsSessionExpired":true') != -1) {
location.reload();
} else {
onAddTilesSuccessful(d);
}
},
error: function () {
errorInOperation();
},
});
});
function onAddTilesSuccessful(e) {
$("#tilesSubmissionMsg").append(e);
}
function errorInOperation(d) {
$("#tilesSubmissionMsg").append("Something went wrong");
}
});
</script>
mvc控制器
[HttpPost]
public ActionResult AddTiles(List<DashboarTileM> userTilesAdd,List<DashboardTileVM> userTilesDelete,int screenWidth)
{
return View();
}
列表模型
public class UserTilesVM
{
public int TileID { get; set; }
public int TopPosition { get; set; }
public int LeftPosition { get; set; }
}
Json数组
"{"userTilesAdd":[{"DashboardTileID":"10"},{"DashboardTileID":"14"},{"DashboardTileID":"18"}],"userTilesDelete":[{"DashboardTileID":"18"}]}"