[英]MVC5 Model Binding from JSON
我在從JSON發布數據中找出模型時遇到麻煩。
JSON:
{
"http://www.xxxx.com/":{
"articulo":[
{
"descripcion":{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
},
"img":{
"innerHTML":"",
"nodeName":"IMG",
"treeDepth":17,
"className":"",
"childNodesLength":0,
"childrenLength":0,
"height":210,
"clientHeight":210,
"parentNodeName":"A",
"parentChildNodeslength":3
}
},
{
"comentarios":{
"innerHTML":"(52)",
"nodeName":"SPAN",
"treeDepth":20,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"DIV",
"parentChildNodeslength":15
}
}
]
}
}
我的模特:
public class GreatClass
{
public IList url { get; set; } = new List<KeyValuePair<string, IList>>();
private IList groups { get; set; } = new List<KeyValuePair<string, IList[]>>();
public IList[] subGroups { get; set; }
private IList metadata { get; set; } = new List<KeyValuePair<string, MetadataJSON>>();
public partial class MetadataJSON
{
public string innerHTML { get; set; }
public string nodeName { get; set; }
public int treeDepth { get; set; }
public string className { get; set; }
public int childNodesLength { get; set; }
public int childrenLength { get; set; }
public Nullable<int> height { get; set; }
public int clientHeight { get; set; }
public string parentNodeName { get; set; }
public int parentChildNodesLength { get; set; }
public string name { get; set; }
}
}
new List<KeyValuePair<string, IList>>();
new List<KeyValuePair<string, IList[]>>();
, = new List<KeyValuePair<string, MetadataJSON>>();
陳述? 因此,遵循該方案並自下而上:
我有點迷茫,因為我認為我的邏輯實現還可以,而且錯誤可能出在JSON的生成上。
當然,我控制器的方法:
[HttpPost]
public ActionResult GetJSONData(GreatClass JSONData)
{
if (ModelState.IsValid)
{
return Json(JSONData);
}
else
{
string errorMessage = "<div class=\"validation-summary-errors\">"
+ "The following errors occurred:<ul>";
foreach (var key in ModelState.Keys)
{
var error = ModelState[key].Errors.FirstOrDefault();
if (error != null)
{
errorMessage += "<li class=\"field-validation-error\">"
+ error.ErrorMessage + "</li>";
}
}
errorMessage += "</ul>";
return Json(errorMessage);
}
}
響應:
您正在發送一個對象,該對象的屬性名為"http://www.xxxx.com/"
,但在C#模型中卻沒有該對象(並且您不能像這樣命名)。 您需要發送具有以下結構的對象:
{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
}
我使用IDictionary和修改JSON“解決了”我的問題(我不知道如何使用原始JSON解決它)。
public class GreatClass
{
public IDictionary<string, IDictionary<string, IDictionary<string, MetadataJSON>[]>> url { get; set; }
public partial class MetadataJSON
{
public string innerHTML { get; set; }
public string nodeName { get; set; }
public int treeDepth { get; set; }
public string className { get; set; }
public int childNodesLength { get; set; }
public int childrenLength { get; set; }
public Nullable<int> height { get; set; }
public int clientHeight { get; set; }
public string parentNodeName { get; set; }
public int parentChildNodesLength { get; set; }
public string name { get; set; }
}
}
和JSON:
{
"url":
[
{
Key: 'http://xxxx.com', Value:
[
{
Key: 'articulos', Value:
[
{
"descripcion":{
"innerHTML":"Newskill Nukite Ratón Gaming MMO/MOBA RGB 16000 DPI",
"nodeName":"SPAN",
"treeDepth":17,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"A",
"parentChildNodeslength":1
},
"img":{
"innerHTML":"",
"nodeName":"IMG",
"treeDepth":17,
"className":"",
"childNodesLength":0,
"childrenLength":0,
"height":210,
"clientHeight":210,
"parentNodeName":"A",
"parentChildNodeslength":3
}
},
{
"comentarios":{
"innerHTML":"(52)",
"nodeName":"SPAN",
"treeDepth":20,
"className":"",
"childNodesLength":1,
"childrenLength":0,
"clientHeight":0,
"parentNodeName":"DIV",
"parentChildNodeslength":15
}
}
]
}
]
}
]
}
請,如果您在不編輯原始JSON的情況下設法解決了該問題,將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.