我尝试通过Ajax将我的网格数据从jQuery传递给MVC Action。 我有一个按钮“保存”我的网页上,这是jQuery的代码,请点击:如何将数组从jQuery Ajax传递给MVC 5 Action?
var result = [];
$('#btnSave').click(function() {
$('#tblMatters tbody tr.mattersRow').each(function() {
var item = {};
if ($(this).find('td.qbmatter > div.dropdown').length > 0) {
item.QBMatter = $(this).find('td.qbmatter > div.dropdown > a').text();
} else {
item.QBMatter = $(this).find('td.qbmatter').text();
}
item.Hours = $(this).find('td.hours').text();
item.Person = $(this).find('td.person').text();
if ($(this).find('td.rate > div.dropdown').length > 0) {
item.Rate = $(this).find('td.rate > div.dropdown > a').text();
} else {
item.Rate = $(this).find('td.rate').text();
}
item.Amount = $(this).find('td.amount').text();
result.push(item);
});
$.ajax({
url: "/Home/SaveQBMatter",
type: "POST",
data: { 'Matters': result },
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) { alert("Success!"); },
error: function() { alert("An error has occured!!!"); }
});
});
我检查了result
阵列,它是正确的。它包含应该是的每个值。 在我的HomeController我有我的数据如下模式:
public class QBMatter
{
public string QBDescription { get; set; }
public string Person { get; set; }
public decimal Hours { get; set; }
public int Rate { get; set; }
public decimal Amount { get; set; }
}
然后将以下操作:
public ActionResult SaveQBMatter (QBMatter[] Matters)
{
DBAccess dba = new DBAccess();
int QBMatterID = 0;
foreach (QBMatter qb in Matters)
{
dba.InsertQBMatter(qb.QBDescription, qb.Person, qb.Hours, qb.Rate, qb.Amount, ref QBMatterID);
}
return RedirectToAction("Home", "Index", "Home");
}
但我始终得到"An error has occured!!!"
结果......我甚至没有得到的行动,所以在AJAX水平上的某个地方... 我做错了什么?
很大的问题,这正是我试图做的,有几乎相同的代码。 –