Is it possible to send a list of javascript objects to a function in the contoller?

2

I am returning a list of data of an object type (GAR) of the actualizarGARs function of the controller:

var listaGARTratadas = db.GAR.ToList();
return Json(listaGARTratadas);

And in javascript I wanted to send this list of objects back to another function ( carregaGARsCriadas ):

$.getJSON("/GAR/actualizarGARs", { carimbo: carimbo, CaixaFisicaGAR: $("#CaixaFisicaGAR").val() },
  function (result) {
        $("#divListagemGARActualizadas").empty();
        $("#divListagemGARActualizadas").load("/GAR/carregaGARsCriadas", {garsCriadas: result});
}

Where do I get this list of objects passed in result :

public ActionResult carregaGARsCriadas(List<GAR> garsCriadas) {
        return PartialView("listaGARsCriadas");
    }

The problem is that I get the garsCriadas variable with the correct number of elements in the object (for example 5), but the content of each element is null     

asked by anonymous 25.08.2014 / 17:48

2 answers

0

The solution I found to my question is to create an array with all the id's that I want to pass to the function in the controller, instead of passing a list of objects.

I then built the array in javascript:

$.getJSON("/GAR/actualizarGARs", { carimbo: carimbo, CaixaFisicaGAR: $("#CaixaFisicaGAR").val() },
        function (result) {                
            var lista = "[";
            var len = result.length;
            $.each(result, function (index, itemData) {
                if (index == len - 1) {
                    lista += "" + itemData.IdGar + "";
                } else {
                    lista += "" + itemData.IdGar + ",";
                }
            });
            lista += "]";

            $("#divListagemGARActualizadas").load("/GAR/carregaGARsCriadas", { garsCriadas: lista });

        });

So the variable lista gets the content [2001, 2002, 3500] for example. From the controller part I received this lista as string and made a split by , :

 public ActionResult carregaGARsCriadas(string garsCriadas)
 {
    if (!(garsCriadas.Contains("undefined")))
    {
       var arrayGARs = garsCriadas.Trim(new char[] { '[', ']' }).Split(',').Select(a => int.Parse(a)).ToList();//.Split
       var listaGARs = db.GAR.Where(g => arrayGARs.Contains(g.IdGar)).ToList();
       return PartialView("listaGARsCriadas", listaGARs);
     }
     return PartialView("listaGARsCriadas");
  }
    
26.08.2014 / 13:19
0

link

You can work with arrays in javascript. Mount the array in your view and then switch to the controller. In the controller opens the array. ;)

    
25.08.2014 / 20:21