Carthorn
Carthorn

Reputation: 43

MVC 3 JSON string not serializing into Controller action

Currently using MVC3 and using jQuery $.post function to send the ajax request to the controller action called "SearchInitiated". I'm having a little bit of a head-scratcher here because I'm not sure exactly where my issues lies. I'm sure its something minor that I have overlooked.

When I call my Controller method from an AJAX call, I am passing a json object (stringified) to a controller action. See below:

Request Headers from Chrome

Accept:/ Content-Type:application/x-www-form-urlencoded; charset=UTF-8 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko)

Chrome/24.0.1312.52 Safari/537.17

X-Requested-With:XMLHttpRequest

Form Dataview

connectorId:1

sessionId:97e2d8b2-be9e-4138-91ed-42ef9c6a2cd6

party: {"Tag":null,"PartyIdentifier":"1","Address":null,"Address2":null,"BusinessName":"","CellPhoneNumber":null,"CIF":"","City":null,"Country":null,"DateOfBirth":null,"EMailAddress":null,"EmploymentTitle":null,"EmployerAddress":null,"EmployerAddress2":null,"EmployerCity":null,"EmployerName":null,"EmployerPhoneNumber":null,"EmployerState":null,"EmployerZip":null,"Fax":null,"Firstname":"TEST","Lastname":"USER","MailingAddress":null,"MailingAddress2":null,"MailingCity":null,"MailingState":null,"MailingZip":null,"Middlename":null,"PhoneNumber":null,"State":null,"TIN":"1111111","WorkPhoneNumber":null,"Zip":null}

javascript

 var parties =    @(Html.Raw(Json.Encode(Model.SearchParties)));
      function SearchForCustomer(id)
      {

      var currentSelectedParty = GetPartyById(id)
      //SearchPost is a wrapper for $.ajax using default values for dataType and contentType
      SearchPost(URL, {
                'connectorId': '@Model.ConnectorId',
                'sessionId': '@Model.SessionId',            
                'party' :  JSON.stringify( currentSelectedParty ) 
            }
      }

Controller

 public ActionResult SearchInitiated(int connectorId, string sessionId, SearchParty party)
 {
     code here....
 }

 public class SearchParty
    {
        public SearchParty();
        public SearchParty(string lastname, string firstname);

        public string Address
        {
            get;
            set;
        }
           public string City
        {
            get;
            set;
        }
        public string Country
        {
            get;
            set;
        }
        public string DateOfBirth
        {
            get;
            set;
        }

        .... etc etc
}

However, the party object is null.

If I change the code to the following, everything deserializes correctly into the strongly typed object.

 public ActionResult SearchInitiated(int connectorId, string sessionId, string party)
 {
     JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            SearchParty sp =
                   json_serializer.Deserialize<SearchParty>(party);
 }

I know my json string is valid since it is working in the second code snippet and the value is being passed in the call. What else could I be missing?

Upvotes: 2

Views: 2922

Answers (5)

Carthorn
Carthorn

Reputation: 43

I resolved my error by modifying the javascript ajax call to this:

 SearchPost(URL, JSON.stringify( {
            'connectorId': '@Model.ConnectorId',
            'sessionId': '@Model.SessionId',            
            'party' :   currentSelectedParty  
        }))

I needed to stringify the entire data object sent in the ajax call, not just the 'party' object

Upvotes: 1

user1961218
user1961218

Reputation:

Try this.

public class SearchParty
{
  public string party { get; set; }

}

  [HttpPost]
  public ActionResult SearchInitiated(SearchParty party)
   {     
       ....
       return View();

  }

Upvotes: 3

Dakait
Dakait

Reputation: 2620

probably you need to set the traditional prop of jQuery.ajax to true in order to achieve traditional style of param serialization

put the below line of code immediatly after the document ready like

$(function(){
 jQuery.ajaxSettings.traditional = true;
});

This SO question may help you further

Upvotes: 2

RGR
RGR

Reputation: 1571

You just need to declare a class 'SearchParty' in the controller to retrieve the strongly typed object in the controller without serializing.

public class SearchParty  
{
 public string party { get; set; }
}

public ActionResult SearchInitiated(SearchParty party)
{
 code here....
}

Please check this link too

Upvotes: 1

Scott Stevens
Scott Stevens

Reputation: 380

I would make sure you have the [Serializable] attribute on your model. Also, make sure your request specifies party={myjson} .

Upvotes: 1

Related Questions