[英]C# Saving Object List Vs Saving Object (From JSON)
I have the following WORKING code: 我有以下工作代码:
Class (Declared Objects) 类(声明的对象)
public class saveRow
{
public int proId { get; set; }
public string proName{get;set;}
}
Controller: 控制器:
[HttpPost]
public virtual JsonResult SaveRow(saveRow input)
{ /* CODE HERE */}
JavaScript Object (Been sent) JavaScript对象(已发送)
var test = {"proId" : 1, "proName" : "Test"}
JavaScript Ajax Call JavaScript Ajax调用
$.ajax({
type: "POST",
url: "URL",
dataType: "json",
data: test,
traditional: true,
success: function (data, status, request) {
if (data.Error != undefined) {
alert("System Error: " + data.Error);
$(row).find('*').attr('disabled', false);
return;
}
},
error: function (request, status, error) {
console.log("ERROR");
}
});
Now the problem occurs when I want to send a list of my rows rather than one at a time so i done the following: 现在,当我想一次发送行列表而不是一次发送行列表时,便出现了问题,因此我执行了以下操作:
To test i took the same object and done 为了测试我拿了相同的对象并完成了
var test2 = []; test2.push(test); test2.push(test);
And the object now looks like: 现在该对象看起来像:
[{"proId" : 1, "proName" : "Test"},{"proId" : 1, "proName" : "Test"}]
My controller now looks like : 我的控制器现在看起来像:
[HttpPost]
public virtual JsonResult SaveRow(List<saveRow> input)
{ /* CODE HERE */}
Also tryed IEnumberable
when sending the list of objects as JSON the varible input is always null. 在将对象列表作为JSON发送时,也IEnumberable
,可变输入始终为null。
But every time i send this list through the controller parameter "input" is always null. 但是每次我通过控制器参数“输入”发送此列表时,始终为空。
Why is this? 为什么是这样?
SOLVED- 解决了-
public virtual JsonResult SaveRow(saveRow[] input)
And added content type! 并添加了内容类型! With JSON.stringify! 使用JSON.stringify!
Try this way: 尝试这种方式:
JS: JS:
var data = { "input": test2 };
$.ajax({
type: "POST",
url: "URL",
dataType: "json",
contentType:"application/json; charset=utf-8", //<--Set content Type
data: JSON.stringify(data), //Set data
success: function (data, status, request) {
if (data.Error != undefined) {
alert("System Error: " + data.Error);
$(row).find('*').attr('disabled', false);
return;
}
},
error: function (request, status, error) {
console.log("ERROR");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.