Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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=user10@email.co 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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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,                           
      })
about 4 years ago · Santiago Trujillo Report

0

Try this:

$.ajax({
  url: urlEditPost,
  method: "POST",
  data: { marketingContactVM: test },
  dataType: "json"
});
about 4 years ago · Santiago Trujillo Report

0

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).

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!