我尝试通过Ajax将我的网格数据从jQuery传递给MVC Action.
我的页面上有一个“保存”按钮,这是点击的jQuery代码:
我的页面上有一个“保存”按钮,这是点击的jQuery代码:
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!!!"); } }); });
我检查了结果数组,这是正确的.它包含应该存在的每个值.
在我的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) { @R_260_4044@ dba = new @R_260_4044@(); 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"); }
但我总是得到“错误已经发生!!!”结果……我甚至没有采取行动,所以在ajax级别的某处出错…
我做错了什么?
解决方法
您需要在发送数据时对数据进行字符串化.
尝试:
$.ajax({ url: "/Home/SaveQBMatter",data: JSON.stringify({ 'Matters': result }),success: function (data) { alert("Success!"); },error: function () { alert("An error has occured!!!"); } });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。