First question, so please forgive any etiquette fails.
I'm working with MVC for the first time and I have a partial view where user can add a 'Visitor' to a 'Visit.' I found another SO q/a that detailed overriding ExecuteResult in JsonResult to use Json.Net for serialization, but as it is I'm still not getting the desired result.
I guess my question is: are my objects not set up correctly for Json.Net to properly serialize the properties that are a collection of complex types, or am I just not serializing them properly?
Here is the 'Visit' Class:
[Serializable]
public class Visit
{
public int Id {get;set;}
public DateTime Date {get;set;}
public List<Visitor> Visitors {get; set;}
}
'Visitor' Class:
[Serializable]
public class Visitor
{
public int Id {get;set;}
public string Name {get;set;}
}
and the ActionResult:
public ActionResult AddVisitor(int VisitId, Visitor visitor)
{
Visit visit = new Visit(visitId);
visit.Visitors.Add(visitor);
var result = new JsonResult()
{
Data = new { ok = true, url = Url.Action("Visitors", "Visit", visit) }
}
}
...that is giving me following url:
http://localhost:56296/Visit/Visitors/1?Date=08%2F14%2F2014%2008%3A57%3A42
&Visitors=System.Collections.Generic.List%601%5BCATSVisitor%5D
(note that the 'visitors' property is there only by name, it does not appear to have serialized the contents)
I assume the serialized string is actually something like:
{
"Id": "1",
"Date": "2014-08-14",
"Visitors":"List<Visitors>"
}
Where I actually want something like:
{
"Id": "1",
"Date": "2014-08-14",
"Visitors": [{"Id" : "1","Name":"Bob"},{"Id" : "2","Name":"Jane"}]
}