Charles
Charles

Reputation: 716

Invalid JSON type MVC 3

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!

Upvotes: 1

Views: 7427

Answers (2)

Charles
Charles

Reputation: 716

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!

Upvotes: 1

ltiong_dbl
ltiong_dbl

Reputation: 3216

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>

Upvotes: 4

Related Questions