我正在 ASP.NET MVC 4 应用程序中动态生成一个表。该表的列中有输入框。我必须保存这些输入框中的数据。生成的表是
单击上面的保存按钮后,我将从 jquery 中的表中读取数据
var estimDetails = $('#editorRows tr:has(td)').map(function (i, v) {
var $td = $('td', this);
return {
ItemId: $($td[0]).find("select").val(),
Description: $($td[1]).find("input").val(),
Quantity: $($td[2]).find("input").val(),
Amount: $($td[3]).find("input").val()
}
}).get();
//Convert data to JSON
var estimateObject = JSON.stringify({ 'JsonString': estimDetails });
//post data to Controller Action
$.ajax({
type: 'POST',
url: '@Url.Action("SaveEstimate")',
data: estimateObject,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data)
alert("Saved Successfully");
},
error: function (response) {
debugger;
alert(response);
}
});
检查“estimateObject”的值为
我的操作方法是
[HttpPost]
public JsonResult SaveEstimate(string JsonString)
{
try
{
DateTime expDate;
DateTime expiryDt = new DateTime();
return Json("true", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("false", JsonRequestBehavior.AllowGet);
}
}
但是“JsonString”始终为空。我尝试了不同的方法,例如将字符串保留在 Controller 的 ViewModel 内,但没有帮助。请帮助我。
最佳答案
要么尝试这样做
[HttpPost]
public JsonResult SaveEstimate(string id)
{
try
{
DateTime expDate;
DateTime expiryDt = new DateTime();
return Json("true", JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json("false", JsonRequestBehavior.AllowGet);
}
}
或在 AJAX 调用中
$.ajax({
type: 'POST',
url: '@Url.Action("SaveEstimate")',
data: "JsonString=" + estimateObject,
dataType: 'json',
success: function (data) {
if (data)
alert("Saved Successfully");
},
error: function (response) {
debugger;
alert(response);
}
});
关于c# - 发送从动态表生成的 JSON 字符串时发布的空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474602/