Pass array of Json objects to Controller C # .NET

1

I need to make a post, and so far I'm trying to get AJAX to send an object to the Controller, but I'm having trouble getting the object.

View / html:

<select name="Servers" id="servers" multiple>
  <option value='{ "Id": "1", "Site": "234", "Tecnologia" : "89" }'>Server 1</option>
  <option value='{ "Id": "12", "Site": "21", "Tecnologia" : "12" }'>Server 2</option>
  <option value='{ "Id": "45", "Site": "332", "Tecnologia" : "56" }'>Server 3</option>
</select>

Javascript / Jquery:

var servers = [];

$("#myForm select:selected").each(function(i){ 
   servers.push(JSON.parse($(this).val()));
 }); 
 
var model = {
  "Nome": "qualquer nome aqui",
  "Servers": servers 
} 

$.ajax({
       type: "POST",
       url: "/Home/Add",
       data: model,
       dataType: "json",
       success: function (msg) {
           //Qualquer código aqui
       }
});

public class EmpresaModel
{
    public string Nome { get;set; }           // Nome está vindo preenchido
    public List<ServerModel> Servers { get;set; }  // Como faço para isso vir preenchido?
}

public class ServerModel
{
    public string Id { get;set; }
    
    public string Site { get;set; }
    
    public string Tecnologia { get;set; }
}

My question would be how do I get this list of objects I created via ajax by the model in the controller? The name is coming and filling in the model, but the list is not. Something like this:

[HttpPost]
public JsonResult Add(EmpresaModel model)
{
    //codigo aqui 
}

NOTE: Code is now only for example, it may have a typo or something.

    
asked by anonymous 19.09.2018 / 01:22

2 answers

0

Your code has some things micro-things that adding up does not make it run, but come on: First:

<select name="Servers" id="servers" multiple>
  <option value='{ "Id": "1", "Site": "234", "Tecnologia" = "89" }'>Server 1</option>
  <option value='{ "Id": "12", "Site": "21", "Tecnologia" = "12" }'>Server 2</option>
  <option value='{ "Id": "45", "Site": "332", "Tecnologia" = "56" }'>Server 3</option>
</select>

The values of your view are not in the default "Key": "Value", note that Technology is using the "=" symbol.

Second, I'm no jquery expert but this part here your selector is picking the select from the select, should it not be the option? I tested with option and it worked out.

$("#myForm select:selected").each(function(i){  ///deveria ser option:selected
   servers.push(JSON.parse($(this).val()));
 }); 

Third, your model does not have the names of your object, so they do not map when they arrive in the controller:

var model = {
  "Nome": "qualquer nome aqui",
  "Servidores": servers    //aqui a chama deveria ser "Servers": servers para se igualar a sua entidade.
} 
    
19.09.2018 / 02:10
0

Friend tries like this:

You have to call this method: JSON.stringify (model).

var servers = [];

$("#myForm select:selected").each(function(i){ 
   servers.push(JSON.parse($(this).val()));
 }); 
 
var model = {
  "Nome": "qualquer nome aqui",
  "Servers": servers 
} 

$.ajax({
       type: "POST",
       url: "/Home/Add",
       data: JSON.stringify(model),
       dataType: "json",
       success: function (msg) {
           //Qualquer código aqui
       }
});
    
10.12.2018 / 18:07