Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda