如何在 .NET MVC 中解析复杂的 JSON
How to parse complex JSON in .NET MVC
我正在通过 ajax 将 JSON 对象传递到我的 MVC 控制器。 JSON 有效。我在第一行的 MVC 控制器中有一个断点。当我检查控制器中的变量 addOnModel 时,基本级别的属性设置正确。 ProductCodeList 属性具有正确数量的元素,但所有值都显示为 null。
我曾尝试更改元素的名称以使它们更独特,但这似乎没有帮助。
{"AddOnID": "0","LocaleID": "63", "Name": "FirstAddOn", "Text": "testText", "ProductCodeList": [ {"AddOnSort": 0, "AddOnProductCode": "product1"}]}
$.ajax({
contentType: 'application/json;',
dataType: 'json',
type: 'POST',
url: '/AddOn/UpsertAddOn',
data: data,
success: function () {
alert('Finished.');
}
});
/// <summary>
/// Object model from web page
//// </summary>
public class AddOnModel
{
public int AddOnID { get; set; }
public int LocaleID { get; set; }
public string Text { get; set; }
public string Name { get; set; }
/// <summary>
/// This is the attribute that won't populate correctly. The number
/// of attributes in the array is correct, but all of its values
/// come across as null. This is what I'm trying to fix.
/// </summary>
public List<AddOnProductModel> ProductCodeList { get; set; }
}
/// <summary>
/// Holds code and sort order.
/// </summary>
public class AddOnProductModel
{
int? AddOnSort { get; set; }
string AddOnProductCode { get; set; }
}
我的控制器片段
/// <summary>
/// Upsert the add on.
/// </summary>
/// <param name="addOnModel">Model from the web page.</param>
/// <returns></returns>
[HttpPost]
public ActionResult UpsertAddOn(AddOnModel addOnModel)
{
AddOn actualAddOn = new AddOn();
if (addOnModel.AddOnID > 0)
{
//Load from DB Repository here.
}
//Rest of my class removed
}
AddOnProductModel 中的属性不是 public。
制作它们public
public class AddOnProductModel
{
public int? AddOnSort { get; set; }
public string AddOnProductCode { get; set; }
}
我正在通过 ajax 将 JSON 对象传递到我的 MVC 控制器。 JSON 有效。我在第一行的 MVC 控制器中有一个断点。当我检查控制器中的变量 addOnModel 时,基本级别的属性设置正确。 ProductCodeList 属性具有正确数量的元素,但所有值都显示为 null。
我曾尝试更改元素的名称以使它们更独特,但这似乎没有帮助。
{"AddOnID": "0","LocaleID": "63", "Name": "FirstAddOn", "Text": "testText", "ProductCodeList": [ {"AddOnSort": 0, "AddOnProductCode": "product1"}]}
$.ajax({
contentType: 'application/json;',
dataType: 'json',
type: 'POST',
url: '/AddOn/UpsertAddOn',
data: data,
success: function () {
alert('Finished.');
}
});
/// <summary>
/// Object model from web page
//// </summary>
public class AddOnModel
{
public int AddOnID { get; set; }
public int LocaleID { get; set; }
public string Text { get; set; }
public string Name { get; set; }
/// <summary>
/// This is the attribute that won't populate correctly. The number
/// of attributes in the array is correct, but all of its values
/// come across as null. This is what I'm trying to fix.
/// </summary>
public List<AddOnProductModel> ProductCodeList { get; set; }
}
/// <summary>
/// Holds code and sort order.
/// </summary>
public class AddOnProductModel
{
int? AddOnSort { get; set; }
string AddOnProductCode { get; set; }
}
我的控制器片段
/// <summary>
/// Upsert the add on.
/// </summary>
/// <param name="addOnModel">Model from the web page.</param>
/// <returns></returns>
[HttpPost]
public ActionResult UpsertAddOn(AddOnModel addOnModel)
{
AddOn actualAddOn = new AddOn();
if (addOnModel.AddOnID > 0)
{
//Load from DB Repository here.
}
//Rest of my class removed
}
AddOnProductModel 中的属性不是 public。
制作它们public
public class AddOnProductModel
{
public int? AddOnSort { get; set; }
public string AddOnProductCode { get; set; }
}