Jquery 为什么我的handsontable的内容没有发布到控制器操作?
我正在尝试将我的handsontable的内容发送到我的控制器操作。表的功能很好,我设法调用了操作,但对象为null。如何将我的handsontable的内容发布到控制器操作?谢谢你的帮助Jquery 为什么我的handsontable的内容没有发布到控制器操作?,jquery,asp.net,ajax,json,asp.net-mvc-3,Jquery,Asp.net,Ajax,Json,Asp.net Mvc 3,我正在尝试将我的handsontable的内容发送到我的控制器操作。表的功能很好,我设法调用了操作,但对象为null。如何将我的handsontable的内容发布到控制器操作?谢谢你的帮助 <script src="../../Scripts/jquery.min.js" type="text/javascript"></script> <script src="../../Scripts/jquery-ui.custom.js" type="text/ja
<script src="../../Scripts/jquery.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui.custom.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.handsontable.full.js" type="text/javascript"></script>
<script src="../../Scripts/numeral.sv-se.js" type="text/javascript"></script>
<link href="../../Content/themes/base/jquery-ui.custom.css" rel="stylesheet" type="text/css" />
<link href="../../Content/themes/base/jquery.handsontable.full.css" rel="stylesheet" type="text/css" />
<link href="../../Content/themes/base/demo-style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
var $container = $("#handsonDataTable");
var $console = $("#handsonDataConsole");
var $parent = $container.parent();
var autosaveNotification;
$container.handsontable({
startRows: 8,
startCols: 6,
rowHeaders: true,
colHeaders: true,
minSpareRows: 1,
contextMenu: true,
});
var handsontable = $container.data('handsontable');
$("button").click(function () {
$.ajax({
url: "/Home/TableData",
data: { "data": handsontable.getData() }, //returns all cells' data
dataType: 'json',
type: 'POST',
success: function (res) {
if (res.result === 'ok') {
$console.text('Data saved');
}
else {
$console.text('Save error');
}
},
error: function () {
$console.text('Saved.');
}
});
});
</script>
<div id="handsonDataTable"></div>
<div id="handsonDataConsole"></div>
<button>Click me</button>
问题出在控制器上。Ajax请求没有问题。您需要修改此代码。尝试将其更改为返回
“hello world”
或其他内容的更简单版本,并检查它是否运行(这样您就不会在ajax中遇到错误500)。您还可以提出与控制器直接相关的新问题。我没有asp.net的技能,所以我只能建议这么做
您可能也会发现有用。
$container.handsontable({
从未关闭过,是打字错误吗?对不起,是的,这是一个打字错误。它已关闭,脚本运行正常。仅在控制器操作中,当我检查datatable操作的参数时,我得到:{datatable |{object}。是否handsontable.getData()
在发送时刻返回正确的数据?将console.log(handsontable.getData())
放在代码中,并从console/firebug中检查它……是否执行了console.log(handsontable.getData())并显示了我的表的内容,所以我猜是getData()这是工作。恐怕我再也帮不上忙了,我对asp.net不太熟悉。最后一个问题是:return View();
returnjson
data?我的意思是已编码,可以发送了。感谢提示Michal…我能够将数据作为逗号分隔的字符串发送到控制器,如下所示:data:myData={data:handsontable.getData().toString()}。因此,问题似乎在于发送数据的格式。我很高兴能提供帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HandsonTable.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
[HttpPost]
public ActionResult TableData(Object tabledata)
{
return View();
}
}
}