0

I'm sending a JSON object to an action method. Everything works client side and the JSON object looks correct but the only values that are set are simple properties. The count on List is always 0.

Here is an example of a JSON object sent to the server. I just alerted the whole JSON string and pasted it below:

{"Tags":"
[{\"Id\":0,\"Title\":\"Windows 8\",\"TagType\":\"Generic\"},{\"Id\":0,\"Title\":\"Dreamweaver\",\"TagType\":\"Generic\"},{\"Id\":0,\"Title\":\"Word\",\"TagType\":\"Generic\"}]",
"CurrentPage":"5",
"ItemsPerPage":"10",
"SearchPhrase":"blaha"}

Here are the C# classes:

public class SearchParams
{
    public List<Tag> Tags { get; set; }
    public string ItemsPerPage { get; set; }
    public string SearchPhrase { get; set; }
    public string CurrentPage { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string TagType { get; set; }
}

Here is the Action method:

 public JsonResult Search(SearchParams searchParams)
 {
    //Stuff happens here
 }

Model binding is working for the three string properties:

ItemsPerPage == 10
SearchPhrase == "blaha"
CurrentPage == 5
Tag.Count == 0 ????  

There should be 3 Tag items here :(

Am I missing something obvious here?

br

Kim

2
  • how does your setup in js look like? using JSON.stringify? Commented Apr 20, 2012 at 9:21
  • Yes i'm using Stringify, the client side was way too complex and would have caused a lot of confusion, so i avoided pasting anything more. There are knockoutjs observables and knockout observable arrays using jquery autocomplete and so on... i'm just glad the problem was quite simple. i was double serializing the objects first with ko.ToJson and then later using JSON.Stringify on the whole object which caused the underlying json-objects in the array to break Commented Apr 20, 2012 at 9:57

2 Answers 2

3

first check your json ..

{     "Tags": " [{\"Id\":0,\"Title\":\"Windows 8\",\"TagType\":\"Generic\"},{\"Id\":0,\"Title\":\"Dreamweaver\",\"TagType\":\"Generic\"},{\"Id\":0,\"Title\":\"Word\",\"TagType\":\"Generic\"}]",     "CurrentPage": "5",     "ItemsPerPage": "10",     "SearchPhrase": "blaha" }

I tested it by json validator there are some error in your json..

    http://jsonlint.com/

http://jsonformatter.curiousconcept.com/

check it..

Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks, i noticed now the json does not look correct, the array object propery names \" are escaped
0

This is old, but for the record a default constructor creating the generic list is what I do and it always works:

public class SearchParams
{
    public List<Tag> Tags { get; set; }
    public string ItemsPerPage { get; set; }
    public string SearchPhrase { get; set; }
    public string CurrentPage { get; set; }

    public SearchParams() {
        Tags = new List<Tag>();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.