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

133
Vistas
Array post through ajax is null in c# method

I am getting an array through a loop and passing it through an ajax call to my c# method but for some reason I am only getting null values in my parameter but count of the array comes through. Im not sure what I am doing wrong. For example the array before passing it through the ajax would have a count of 3, each key populated with the needed info, but as I pass it through the ajax call and debug on VS, the parameter for my list is null but has a count of 3?

let test = $('.divtest').map(function (index, element) {

    return {
        key1: $(element).closest('.testdiv')[0].id,
        key2: $(element).attr('id'),
        key3: $(element).attr('style')
    }
}).get();

Ajax Call

$.ajax({
    cache: false,
    type: "POST",
    data: { test: test},
    dataType: "json",
    url: "/controller/testhere",
    success: function (result) {
      alert('done');
    }
});

C# Method

 public JsonResult testhere(List<string> test)
    {
       //stuff
        return null;
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

your action is a list of string but ajax data is not. try this data

let test = [ "key1", "key2", "key3"];
.....
.ajax({
    cache: false,
    type: "POST",
    data: { test: test},
    dataType: "json",
    url: "/controller/testhere",
    success: function (result) {
      alert('done');
    }
});

or you will have to change the action

Create view model

public class TestViewModel
{
public string Key1 {get; set;}
public string Key2 {get; set;}
public string Key3 {get; set;}
}

action

public JsonResult testhere(TestViewModel test)
about 4 years ago · Juan Pablo Isaza 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