javascript - 无法将html表数据绑定(bind)到mvc Controller 模型

标签 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发布表格数据,并将其发送到 Controller

 $("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"}]}

但是,当我检查 Controller 时,列表不包含任何元素,并且没有绑定(bind)任何值,我检查了几篇文章,但我无法弄清楚将 json 数据绑定(bind)到模型中的错误是什么 Controller

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


        return Json("Success");
    }

最佳答案

编辑

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

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

应该是

  data: JSON.stringify({ Model : itemlists}),

看起来这只是数组名称的拼写错误。

工作代码:

@{
    ViewBag.Title = "Home Page";
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>
<script type="text/javascript">
    $("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({
            url: './Home/Create',
            type: 'POST',
            data: JSON.stringify({ Model: itemlists }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                alert(r + " record(s) inserted.");
            },
            error: function (r) {
                alert(JSON.stringify(r));
            }
        });
    });
</script>

<input type="text" id="txtShipmentID" />
<table id="tblAttachShip">
    <tbody>
        <tr>
            <td>aaa</td>
            <td>aa</td>
            <td>a</td>
        </tr>
        <tr>
            <td>bbb</td>
            <td>bb</td>
            <td>b</td>
        </tr>
    </tbody>
</table>

<button id="btnSave">Save</button>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

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


            return Json(new { Message = "Success" });
        }
    }


    public class Model
    {
        public string itemlineId { get; set; }
        public string shipmentID { get; set; }
        public string containerID { get; set; }
        public string containerType { get; set; }
    }
}

关于javascript - 无法将html表数据绑定(bind)到mvc Controller 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60190626/

相关文章:

jquery - CSS 照片库 float 问题 - 混合风景和肖像照片

c# - 存储在同一位置的 Azure 缓存中的 session 在多个实例上不同步

c# - 为什么这个多态的 C# 代码会打印出它的作用?

c# - MySQL C#,日期时间列的最后 x 分钟

javascript - 单击特定按钮以显示特定 ID

本地和 JSBin 之间的 JQuery Mobile Grid 行为不同

javascript - HTML5 视频播放/淡出然后触发下面的视频

使用 Javascript 更新 PHP

javascript - 了解 JS try 和 catch 语句(用于添加自定义 Modernizr 测试)

javascript - IE 11、10 和 9 中的图像缩放很糟糕