AlexGH
AlexGH

Reputation: 2805

Ajax Post request MVC

I have this JQuery function:

function EditContactPost()
    {
        var urlEditPost = $("#hdnInfo").data("url_add_contact");
        var test = $("#formEditContact").serialize();        
        alert(test);
        $.ajax({
            type: 'post',
            url: urlEditPost,                    
            data:{ marketingContactVM: test },                           
        })
    }

I'm doing an alert as you see and I'm watching that the test variable has the form serialized correctly with all the values. But when the controller action receives this request on server side marketingContactVM is always null. Why is this happening? My controller action looks like this:

 [HttpPost]
        public ActionResult AddContact(MarketingVM marketingContactVM) //always null
        {
some code...
        }

Just in case this is my model class, it contains another list inside:

     public class MarketingVM
        {
            public IEnumerable<string> States { get; set; }  
            public List<MarketingVM> MarketingList { get; set; }           
            public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string Company { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public string Country { get; set; }
            public string Phone { get; set; }
            public string Fax { get; set; }
            public string Mail{ get; set; }
            public string URL { get; set; }
            public bool IsTrue{ get; set; }
            public int OtherId{ get; set; }
    }

I've checked the Network tab in DevTools and this is the Request URL:http://localhost:56602/ControllerName/AddContact , that should be fine. But the data is being sent as a FormData, like this:

    marketingContactVM:Company=Microsoft+Company&Email=user10%40email.co&First=
Auto&Last=Mapper&Phone=98797988997&Fax=&URL=ww.auto.org&Adress=automapper+
street&City=auto+city&State=FL&Zip=33654&Country=USA&MarketingContacts%5B0%5D
.Name=1%2F03%2F2012+1%3A15+PM&MarketingContacts%5B0%5D.IsSelected=false

.. and like that. Maybe the way the data is being sent.. I think that the correct format should be:

marketingContactVM: Company=Microsoft Company [email protected] First=Charles Last=Johnston Phone=98797988997 Fax=989898 URL=ww.auto.org Adress=North streetCity=Sacramento State=FL Zip=33654 Country=USA MarketingContacts.Name=Piotr MarketingContacts.IsSelected=false

Upvotes: 2

Views: 58

Answers (3)

Usman
Usman

Reputation: 4703

since you are serializing the form you dont need to add it in json object

 data:{ marketingContactVM: test }, 

instead just pass the serialized object like this

$.ajax({
          type: 'post',
          url: urlEditPost,                    
          data:test,                           
      })

Upvotes: 2

Cristian Szpisjak
Cristian Szpisjak

Reputation: 2469

Add the contentType property. This way AP.NET MVC will know to deserialize the request body as JSON.

function EditContactPost()
{
    var urlEditPost = $("#hdnInfo").data("url_add_contact");
    var test = $("#formEditContact").serialize();        
    alert(test);
    $.ajax({
        type: 'post',
        url: urlEditPost,                    
        data:{ marketingContactVM: test },
        contentType: 'application/json'                           
    })
}

PS: Also you should use data: JSON.stringify(test).

Upvotes: 0

Kumar Nitesh
Kumar Nitesh

Reputation: 1652

Try this:

$.ajax({
  url: urlEditPost,
  method: "POST",
  data: { marketingContactVM: test },
  dataType: "json"
});

Upvotes: 0

Related Questions