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

109
Visualizações
Post select2 multiple values using form-data to controller

How to post select2 multiple values using form-data?

because form-data returns like below:

pid=0&members=staff1&members=staff2&members=staff7

members value would be a very dynamic value since it's multiple select, how to deal with this? I have tried to breakdown the select2 result and JSON.stringify first then pass it to the controller, it arrived at the controller but the value was null.

<div class="form-group">
    <label class="col-form-label" for="input-members">Selected Members</label>
    <select class="form-control select2" id="members" name="members[]" multiple="multiple"> 
    </select>
 </div> 

 const _u = [];
 for (var i = 0; i <= users.length - 1; i++) {
      console.log(users[i]);

     var j = {
         id: users[i]
     };

     _u.push(j);
}

var _f = JSON.stringify({
    pid: 0,
    members: _u
});

$.ajax({
     type: 'POST',
     url: '/User/Update/',
     traditional: true,
     data: _f,
     contentType: "application/json; charset=utf-8",
     dataType: 'json',
     headers: headers,
     
 [HttpPost]
 [Route("/User/Update")]
 [AllowAnonymous]
 public JsonResult Update_Data(string _f)

 // it arrived here, but _f value was null

what I missed?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

since you are using json content type, you have to add FromBody, and if you are using newtonsoft json, instead of string I would suggest to use JObject

public JsonResult Update_Data([FromBody]JObject _f)

if you still want to use your action with string you have to use this ajax (remove content type)


$.ajax({
     type: 'POST',
     url: '/User/Update/',
     traditional: true,
     data:{ "_f" : _f },
     dataType: 'json',
    
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