我有一个复杂的json结构。这里简化了,因为它也是这样:
{
"Id":0,
"Name":"Region Challenge",
"ModifiedOn":"2011-09-08T17:49:22",
"State":"Published",
"Goals":[
{"Id":1,"Description":"some text here","DisplayOrder":1},
{"Id":2,"Description":"some text here","DisplayOrder":2}
]
}
因此,当这个数据被POST给控制器时,我可以获得Id,Name等的这个值没有问题。但是,当我查看locals窗口时,Goals为null。
签名是:
public JsonResult Save(int Id, String Name, DateTime ModifiedOn, String State, String Goals)
POST标题是:
User-Agent: Fiddler
Host: localhost:2515
Content-Type: application/json
Content-Length: 7336
如何读取数据以便我可以迭代它?
谢谢! 埃里克
答案 0 :(得分:1)
您的Goals
是一个数组或列表。
public class SomeThing
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime ModifiedOn { get; set; }
public string State { get; set; }
public List<Goal> Goals { get; set; }
}
public class Goal
{
public int Id { get; set; }
public string Description{ get; set; }
public int DisplayOrder{ get; set; }
}
public JsonResult Save(SomeThing model)
{
// model.Name ....
// model.Id ...
// model.Goals is your list of Goals
// return Json
}
答案 1 :(得分:0)
定义类似
的类public class MyClass{
public int Id{get;set;}
public string Name {get;set;}
public string State {get;set;}
public IList<Goals> Goals {get;set;}
}
您的目标类看起来像
Public class Goals{
public int Id{get;set;}
public string Description {get;set;}
public int DisplayOrder {get;set}
}
之后就像
那样收到了它public JsonResult Save(MyClass _MyClass)
你必须包括param名称,就像你通过ajax发送它一样
data:{_MyClass: yourJSON}