【发布时间】:2014-04-30 12:55:35
【问题描述】:
我正在尝试将我的掌上电脑的内容发送到我的控制器操作。该表功能正常,我设法调用了该操作,但该对象为空。如何将我的handsontable 的内容发布到我的控制器操作中?感谢您的所有帮助!
<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>
在我的控制器中:
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();
}
}
}
【问题讨论】:
-
$container.handsontable({永远不会关闭,是错字吗? -
对不起,是的,这是一个错字。它已关闭,脚本运行良好。就在控制器动作中,当我检查 DataTable 动作的参数时,我得到:{datatable|{object}。
-
handsontable.getData()是否在发送时刻返回正确的数据?将console.log(handsontable.getData())放入您的代码中并从控制台/firebug 中检查它。 -
-
恐怕帮不上忙了,我对asp.net不熟悉。最后一个问题是:
return View();是否返回json数据?我的意思是编码,准备发送。
标签: jquery asp.net ajax json asp.net-mvc-3