javascript - 无法将 html 表数据绑定到 mvc 控制器模型
问题描述
我有这个模型
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");
}
解决方案
编辑
我想我们俩都有点过火了。我做了一些挖掘,看起来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; }
}
}
推荐阅读
- python - 无法使用基于 python 的库 ftplib 连接到本地 FTP 服务器
- java - 无法连接到 postgres Scala slick org.postgresql.util.PSQLException:连接尝试失败
- javascript - 如何使用 html-webpack-plugin 在我的 webpack 模板 html 中包含脚本标签
- c# - 从给定键和值的字典数组中获取字典的索引
- azure-active-directory - Microsoft Graph - 如何为用户扩展属性“过滤”
- python - 使用 LCG 方法生成随机数
- flutter - 构建模块“nanopb”时无法构建 iOS 应用程序
- rest - 谁能理解我的托管heroku问题
- sql - 在 oracle 中同时执行 when 和 else 部分时执行 case
- asp.net-core - 在作为 Windows 服务运行的自包含 .Net Core Web 应用程序中的嵌套 DLL 引用问题