2

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Carthorn
  • 43
  • 1
  • 6
  • 1
    Can you send us an example request that you are sending via post? – Scott Stevens Jan 08 '13 at 22:23
  • Why not just let the framework do the work for you? Change the signature to this: `public ActionResult SearchInitiated(int connectorId, string sessionId, SearchParty party)` and you have't to deserialize manualy. If this doesn't work, you have a problem, so post the data that you are sending to this action. – Ricardo Souza Jan 08 '13 at 22:33
  • Also, I see you specified the "full" class name in the declaration but not in the deserialization. Are there any other classes called `SearchParty` in this context? – Ricardo Souza Jan 08 '13 at 22:35
  • I've updated the question a little with some more information – Carthorn Jan 10 '13 at 22:00

5 Answers5

3

Try this.

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

}

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

  }
2

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

Community
  • 1
  • 1
Dakait
  • 2,531
  • 1
  • 25
  • 43
1

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

Scott Stevens
  • 390
  • 1
  • 7
1

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

Community
  • 1
  • 1
RGR
  • 1,521
  • 2
  • 22
  • 36
1

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

Carthorn
  • 43
  • 1
  • 6