1

I searched all over the web and I think I'm correctly using JSON, I just can't find what's the problem with it. I need to pass this json object as the parameter of an MVC controller action:

JSON:

{ 
"ContactId": "0",
"Fax2": "dsad",
"Phone2": "dsad",
"Tittle": "asdsa",
"Cellphone": "dsadsd", 
"Address": { "City": "kjshksdfks",
             "Country": "undefined",
             "Id": "0",
             "State": "dsadsa"}
}

The .NET object is as follows:

public class Contact
{
    public string ContactId {get; set;}
    public string Fax2 {get; set;}
    public string Phone2 {get; set;}
    public string Tittle {get; set;}
    public string Cellphone {get; set;}
    public Address ContactAddress {get; set;}
}

The nested address type is as follows:

public class Address
{
    public string City {get; set;}
    public string Country {get; set;}
    public int Id {get; set;}
    public string State {get; set;}
}

Whats wrong with my JSON?, what am i missing in order that passes clean to the controller action

The exact error is: Invalid JSON primitive: Contact

Note: I tried using a javascript object, JSON.stringify, and toJSON()

Thanks in advance!

nvuono
  • 3,323
  • 26
  • 27
Charles
  • 696
  • 1
  • 10
  • 19
  • You seem to be missing a quote here: `"City": kjshksdfks"` Is that just a typo in your question or in your actual code? – Joel C May 14 '12 at 03:16
  • I have edited your post to fix the typo in the JSON string based on your comment to my original answer. Are you using Jquery to send the json object in an $.ajax post? Could you show us that code? – nvuono May 14 '12 at 07:23
  • Well, I don't know if you tried, but just put that in a javascript var and send it in the data field of the AJAX call. That should do the trick. I have several of those in my project and it works like a charm. Check if you have all MVC scripts imported though. – oamsel May 14 '12 at 07:54

2 Answers2

4

I suspect you were missing the content type for the ajax call. See also: http://forums.asp.net/t/1665485.aspx/1?asp+net+mvc+3+json+values+not+recieving+at+controller.

I've changed a couple of things 1) Tittle -> Title, and Address -> ContactAddress; This should work:

JSON:

{
  "ContactId": "a",
  "Fax2": "b",
  "Phone2": "c",
  "Title": "d",
  "Cellphone": "e",
  "ContactAddress": {
      "Id": 22,
      "City": "a",
      "State": "b",
      "Country": "c"
    }
}

CALL:

<script>
    var contact = { "ContactId": "a", "Fax2": "b", "Phone2": "c", "Title": "d", "Cellphone": "e", "ContactAddress": { "Id": 22, "City": "a", "State": "b", "Country": "c"} };

    $.ajax({
        type: 'POST',
        url: '[YOUR-URL]',
        data: JSON.stringify(contact),
        success: function (result) {
            alert('success');
        },
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });
</script>
ltiong_sh
  • 3,186
  • 25
  • 28
  • Now that you pointed it out I'm pretty sure the Address -> ContactAddress change was specifically causing the Invalid JSON Primitive error. You can't just map a JSON item named Address to a type named Address. It has to map to the actual variable named ContactAddress (of type Address) as you've shown – nvuono May 14 '12 at 08:26
  • actually..i tested that too. It will still map. ContactAddress will just be null. – ltiong_sh May 14 '12 at 08:31
  • In fact the ajax Call is like you post, but reviewing your code and mine i find the problem and its because im passing 2 parameters to the controller actions. So the question now is, whats wrong with the ajax call that does not recognize the 2 parameters. Strangely it does not mark a parameter error, but a Invalid JSON type. – Charles May 14 '12 at 14:41
1

Done, i share the solution:

Since the petition is being made indicating that is of type "json" the additional types must be inside the json, otherwise the routing engine takes it as a non json object and for that reason marks that the json is not valid, so, in my case i do this:

{
  "ContactId": "a",
  "Fax2": "b",
  "Phone2": "c",
  "Title": "d",
  "Cellphone": "e",
  "ContactAddress": {
      "Id": 22,
      "City": "a",
      "State": "b",
      "Country": "c"
    }
},{"provider": 1}

Altought its not required any additional configuration, for passing the additional parameter since were working with a JSON, in a different scenario that uses .NET objects we must specify that the controller can accept many parameters. So, for do this we must configure the controller routes:

context.MapRoute(
    "Providers_default",
    "Catalogs/{controller}/{action}/{id1}/{id2}",
    new { controlller = "Provider", action = "Index", id = UrlParameter.Optional }
);

Here's a link: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

Best Regards!

Charles
  • 696
  • 1
  • 10
  • 19
  • I just found another blog that describes similarly this, hope it helps. http://stackoverflow.com/questions/2246481/routing-with-multiple-parameters-using-asp-net-mvc Best regards! – Charles May 22 '12 at 15:27