Javascript 无法将html表数据绑定到mvc控制器模型

Javascript 无法将html表数据绑定到mvc控制器模型,javascript,c#,jquery,asp.net-mvc,asp.net-ajax,Javascript,C#,Jquery,Asp.net Mvc,Asp.net Ajax,我有这个型号 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发布表数据,并将其发送给控制器 $

我有这个型号

 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.");
        }
    });
});
到目前为止一切正常,当我尝试在浏览器中读取post请求时,我可以看到请求具有正确的json格式数据

Model: [{itemlineId: "aa", shipmentID: "a", containerID: "aa", containerType: "aa"}]}
然而,当我检查控制器时,列表不包含任何元素,并且没有绑定任何值,我检查了几篇文章,但我无法找出将json数据绑定到控制器中的模型的错误

 [HttpPost]
    public JsonResult Create(List<Model> Model)
    {


        return Json("Success");
    }
[HttpPost]
公共JsonResult创建(列表模型)
{
返回Json(“成功”);
}
编辑

我想我们俩都有点过火了。我做了一些挖掘,看起来由于ajax发布数据的方式,
JSON.stringify
实际上是必要的;我认为您的问题实际上可能与javascript有关。当我复制你的代码时,出现了错误。我正在运行这个正在运行。假设您发布的代码是复制和粘贴的,这:

data:JSON.stringify({Model:Itemslists}),
应该是

data:JSON.stringify({Model:itemlists}),
看起来这只是一个数组名称的输入错误

工作代码:

@{
ViewBag.Title=“主页”;
}
$(“正文”)。在(“单击”,“保存”,函数(){
//循环遍历表行并构建JSON数组。
var itemlists=新数组();
$(“#t车床车斗TR”)。每个(功能){
var行=$(此);
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);
});
//使用AJAX将JSON数组发送到控制器。
$.ajax({
url:“./Home/Create”,
键入:“POST”,
数据:JSON.stringify({Model:itemlists}),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(r){
警报(r+“插入记录”);
},
错误:函数(r){
警报(JSON.stringify(r));
}
});
});
aaa
aa
A.
bbb
bb
B
拯救
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间WebApplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
关于()的公共行动结果
{
ViewBag.Message=“您的应用程序描述页。”;
返回视图();
}
公共行动结果联系人()
{
ViewBag.Message=“您的联系人页面。”;
返回视图();
}
[HttpPost]
公共JsonResult创建(列表模型)
{
返回Json(新的{Message=“Success”});
}
}
公共类模型
{
公共字符串itemlineId{get;set;}
公共字符串shipmentID{get;set;}
公共字符串容器ID{get;set;}
公共字符串容器类型{get;set;}
}
}
编辑

我想我们俩都有点过火了。我做了一些挖掘,看起来由于ajax发布数据的方式,
JSON.stringify
实际上是必要的;我认为您的问题实际上可能与javascript有关。当我复制你的代码时,出现了错误。我正在运行这个正在运行。假设您发布的代码是复制和粘贴的,这:

data:JSON.stringify({Model:Itemslists}),
应该是

data:JSON.stringify({Model:itemlists}),
看起来这只是一个数组名称的输入错误

工作代码:

@{
ViewBag.Title=“主页”;
}
$(“正文”)。在(“单击”,“保存”,函数(){
//循环遍历表行并构建JSON数组。
var itemlists=新数组();
$(“#t车床车斗TR”)。每个(功能){
var行=$(此);
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);
});
//使用AJAX将JSON数组发送到控制器。
$.ajax({
url:“./Home/Create”,
键入:“POST”,
数据:JSON.stringify({Model:itemlists}),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(r){
警报(r+“插入记录”);
},
错误:函数(r){
警报(JSON.stringify(r));
}
});
});
aaa
aa
A.
bbb
bb
B
拯救
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间WebApplication1.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
关于()的公共行动结果
{
ViewBag.Message=“您的应用程序描述页。”;
返回视图();
}
公共行动结果联系人()
{
争夺
data: JSON.stringify({ Model : Itemslists}),
data: Itemslists,
(List<Model>)serializer.Deserialize(jsonString, typeof(List<Model>);