0

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"}]
}
Community
  • 1
  • 1
Goldie
  • 1
  • 3
  • Seems like this has less to do with JSON.NET and more to do with the URL that `Url.Action` is generating. – Andrew Whitaker Aug 07 '14 at 13:44
  • Oh, this actually posted...I kept getting 'an error occurred when submitting the question' and editing/retrying. Hope I didn't accidentally spam. Lack of get/set was an omission in the post, I'll update it @AndrewWhitaker I'll verify that serializing it without anything else going on correctly captures the collection – Goldie Aug 07 '14 at 13:45
  • Look at the result of your `Url.Action` call *without* serializing it to JSON and I bet it will have the same issues – Andrew Whitaker Aug 07 '14 at 13:51
  • Oh, yeah, I kind of missed the URL part. Why are you trying to serialize this data into a querystring? We might be able to suggest a different approach. – SethMW Aug 07 '14 at 13:52
  • I just did JsonConvert.SerializeObject() by itself and verified that it correctly captured the complex property. @SethMW This not being a discussion site I hesitate to even get into it here. Long story short is I've never touched MVC before this week, there is one guy in the shop that has who will be around next week, I was just throwing together a demo that was intended to show off bootstrap, this is just faked functionality. I have a Visit view that will display visit summary and list of visitors, when you click 'add' it renders a partial view which, when saved, gets here – Goldie Aug 07 '14 at 13:59
  • @AndrewWhitaker right you are. string s = Url.Action(a,c,visit) gives me the exact thing I see above, so it's evidently not serializing it at all. If I try serializing it in advance and supplying the result to Url.Action(), the url ends up being /Visit/Visitors?Length=939 – Goldie Aug 07 '14 at 14:07

0 Answers0