将JSON发布到控制器MVC 5 [英] Posting JSON to controller MVC 5
问题描述
我正在将JSON发布到我的控制器,但是问题是我在列表中得到了正确的计数.即如果JSON有两个元素,则list的计数为2但数据为空.以下是通过我制作并发送JSON的代码.我已经使用TabletoJSON制作了JSON.
I am posting JSON to my controller, but the problem is I am getting the correct count in list. i.e if JSON has two elements list has count of two but null data. Following is the code via I am making and sending the JSON. I've use TabletoJSON for making the JSON.
$('#productsObj').click(function () {
var rawMaterials = $('#productsTable').tableToJSON(
{
ignoreColumns: [3]
});
alert(JSON.stringify(rawMaterials));
console.log(rawMaterials);
$.ajax({
url: "/Supplies/insertRawMaterial",
type: "POST",
data: JSON.stringify(rawMaterials),
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(data);
alert(response);
}
});
});
以下是正在接收数据的控制器操作方法.
Following is the controller action method which is receiving data.
public ActionResult insertRawMaterial(List<String> data)
{
if (data != null)
{
return Json("Success");
}
else
{
return Json("An Error Has occoured");
}
}
我不确定我在哪里做错了.以下是警报中的JSON.
I am not sure where I am doing it wrong. Following is the JSON in alert.
[{"Raw Material Name":"Asphalt","Short Code":"AS02","Price":"20"}]
推荐答案
您不可能期望将此复杂的JSON结构映射到控制器操作当前将其用作参数的字符串列表(List<String>
):
You cannot possibly expect to map this complex JSON structure to a list of strings (List<String>
) that your controller action currently takes as parameter:
[{"Raw Material Name":"Asphalt","Short Code":"AS02","Price":"20"}]
因此,您可以先定义适当的视图模型以反映该结构(或者几乎,请参阅下面的按摩技术):
So you may start by defining the appropriate view models that will reflect this structure (or almost, see the massaging technique required below):
public class MaterialViewModel
{
public string Name { get; set; }
public string ShortCode { get; set; }
public decimal Price { get; set; }
}
您的控制器操作将其作为参数:
which your controller action will take as parameter:
public ActionResult insertRawMaterial(IList<MaterialViewModel> data)
{
...
}
最后在客户端上处理您的数据以匹配正确的属性名称(名称,shortCode和价格当然):
and finally massage your data on the client to match the correct property names (name, shortCode and price of course):
// we need to massage the raw data as it is crap with property names
// containing spaces and galaxies and stuff
var massagedRawMaterials = [];
for (var i = 0; i < rawMaterials.length; i++) {
var material = rawMaterials[i];
massagedRawMaterials.push({
name: material['Raw Material Name'],
shortCode: material['Short Code'],
price: material['Price']
});
}
$.ajax({
url: "/Supplies/insertRawMaterial",
type: "POST",
data: JSON.stringify(massagedRawMaterials),
contentType: "application/json; charset=utf-8",
...
这就是说,如果您当前正在使用的客户端库产生了这种不确定的属性名称,那么您可以考虑用更合适的名称替换它.
This being said, if the client side library you are currently using produces such undeterministic property names, you might consider replacing it with something more appropriate.
这篇关于将JSON发布到控制器MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!