[英]Null parameter in controller from ajax
When a user clicks a save button, a JavaScript function uses AJAX to call the Controller and send over JSON data about the objects. 当用户单击保存按钮时,JavaScript函数使用AJAX调用Controller并通过对象发送有关JSON的数据。
JavaScript Function JavaScript功能
$.ajax({
url: "/Data/sendFridgeItems?items=" + JSON.stringify($scope.items.data),
type: "POST",
data: JSON.stringify($scope.items.data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log("good!");
},
error: function () {
console.log("error");
}
});
Controller 调节器
[HttpPost]
public ActionResult SendFridgeItems(string items)
{
fridge[] fridgeItems = JsonConvert.DeserializeObject<fridge[]>(items);
foreach (fridge item in fridgeItems)
{
bool exists = cookDB.fridges.AsEnumerable()
.Any(x => x.Name == item.Name && x.Purchased == item.Purchased && x.Count == item.Count);
if (!exists)
{
cookDB.fridges.Add(item);
cookDB.SaveChangesAsync();
}
}
return null;
}
It works, but I don't think the way of sending my data through the url parameter is correct in my situation, because the data will be big enough. 它可以工作,但是在我的情况下,我认为通过url参数发送数据的方式不正确,因为数据足够大。 I wanted to know if there is a better way to send my data to the controller?
我想知道是否有更好的方法将数据发送到控制器?
I tried to send it this way, but the controller receives null value. 我试图以这种方式发送它,但是控制器接收到空值。
$.ajax({
url: "/Data/sendFridgeItems",
type: "POST",
data: JSON.stringify($scope.items.data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log("good!");
},
error: function () {
console.log("error");
}
});
JSON of $scope.items.data $ scope.items.data的JSON
[{"id":2,"name":"Item1","count":2,"type":"pcs.","purchased":"12/09/2017","wasted":"15/10/2017","cam":"Freezer","comment":"no comment","$$hashKey":"object:38"},{"id":3,"name":"Item2","count":30,"type":"g.","purchased":"15/01/1880","wasted":"21/03/1882","cam":"Cooler","comment":"commented","$$hashKey":"object:39"}]
$scope.items $ scope.items
$scope.items = {
"count": 2,
"data": [
{
"name": "Item1",
"count": 2,
"type": "pcs.",
"purchased": "12/09/2017",
"wasted": "15/10/2017",
"cam": "Freezer",
"comment": "no comment"
},
{
"name": "Item2",
"count": 30,
"type": "g.",
"purchased": "15/01/1880",
"wasted": "21/03/1882",
"cam": "Cooler",
"comment": "Commented"
}
]
};
Fixed Controller For N.Ivanov's solution (this controller+ N.Ivanov's ajax = solution) 用于N.Ivanov解决方案的固定控制器(此控制器+ N.Ivanov的ajax =解决方案)
public ActionResult SendFridgeItems(fridge[] items)
{
fridge[] fridgeItems = JsonConvert.DeserializeObject<fridge[]>(items.ToString());
foreach (fridge item in items)
{
bool exists = cookDB.fridges.AsEnumerable()
.Any(x => x.Name == item.Name && x.Purchased == item.Purchased && x.Count == item.Count);
if (!exists)
{
cookDB.fridges.Add(item);
cookDB.SaveChangesAsync();
}
}
return null;
}
The data
field in ajax takes in an Object, you are giving it a string. ajax中的
data
字段接受一个Object,您为其提供了一个字符串。 Try and supply only your object, assuming that $scope.items.data
is an object. 假设
$scope.items.data
是一个对象,请尝试仅提供您的对象。 If you give a bit more information on what $scope
variable is, then I can give you a better answer. 如果您提供有关
$scope
变量什么的更多信息,那么我可以给您一个更好的答案。
Code: 码:
$.ajax({ url: "/Data/sendFridgeItems", type: "POST", d̶a̶t̶a̶:̶ ̶$̶s̶c̶o̶p̶e̶.̶i̶t̶e̶m̶s̶.̶d̶a̶t̶a̶,̶ dataType: "json", contentType: "application/json; charset=utf-8", success: function () { console.log("good!"); }, error: function () { console.log("error"); } });
Hope this helps! 希望这可以帮助!
EDIT: 编辑:
Further inspection after you have provided the contents of $scope.items.data
led me to notice that $scope.items.data
is an array of objects. 提供
$scope.items.data
的内容后的进一步检查使我注意到$scope.items.data
是对象数组。 So in order for the ajax to work and actually supply valid JSON, try the following code: $.ajax({ url: "/Data/sendFridgeItems", type: "POST", data: { "items": $scope.items.data }, dataType: "json", contentType: "application/json; charset=utf-8", success: function () { console.log("good!"); }, error: function () { console.log("error"); } });
因此,为了使ajax正常工作并提供有效的JSON,请尝试以下代码:
$.ajax({ url: "/Data/sendFridgeItems", type: "POST", data: { "items": $scope.items.data }, dataType: "json", contentType: "application/json; charset=utf-8", success: function () { console.log("good!"); }, error: function () { console.log("error"); } });
I have verified that { "item": $scope.items.data }
is a valid JSON through JSONLint 我已经通过JSONLint验证了
{ "item": $scope.items.data }
是有效的JSON
Hope this solves your problem! 希望这能解决您的问题!
Try JSON Parse to parse data as Object and send it to controller 尝试JSON Parse将数据解析为Object并将其发送到控制器
$.ajax({
url: "/Data/sendFridgeItems",
type: "POST",
data: JSON.parse($scope.items.data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
console.log("good!");
},
error: function () {
console.log("error");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.