Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Try this:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda