【发布时间】:2020-05-28 03:30:00
【问题描述】:
我有这个模型
public class Model
{
public string itemlineId { get; set; }
public string shipmentID { get; set; }
public string containerID { get; set; }
public string containerType { get; set; }
}
我有一个动态的 html 表格,我想做的是通过 ajax 发布表格数据,并将其发送到控制器
$("body").on("click", "#btnSave", function () {
//Loop through the Table rows and build a JSON array.
var itemlists= new Array();
$("#tblAttachShip TBODY TR").each(function () {
var row = $(this);
var itemList = {};
itemList.itemlineId = row.find("TD").eq(0).html();
itemList.shipmentID = document.getElementById("txtShipmentID").value
itemList.containerID = row.find("TD").eq(1).html();
itemList.containerType = row.find("TD").eq(2).html();
itemlists.push(itemList);
});
//Send the JSON array to Controller using AJAX.
$.ajax({
type: "POST",
url: "/Project/Create",
data: JSON.stringify({ Model : Itemslists}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r + " record(s) inserted.");
}
});
});
到目前为止一切正常,当我尝试在浏览器中读取发布请求时,我可以看到该请求具有正确的 json 格式数据
Model: [{itemlineId: "aa", shipmentID: "a", containerID: "aa", containerType: "aa"}]}
但是,当我检查控制器时,列表不包含任何元素,并且没有绑定任何值,我检查了几篇帖子,但我无法弄清楚我在将 json 数据绑定到模型时做错了什么控制器
[HttpPost]
public JsonResult Create(List<Model> Model)
{
return Json("Success");
}
【问题讨论】:
标签: javascript c# jquery asp.net-mvc asp.net-ajax