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

Asked

Viewed 142 times

2

I’m returning a data list of an object type (GAR) function actualizarGARs controller’s:

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

And in javascript I wanted to send this list of objects again 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 on result:

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

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

  • Knows the BeginCollectionItem? http://answall.com/questions/15804/d%C3%Bavida-em-constru%C3%A7%C3%A3o-de-view-e-controller-with-entity-dependent-of-cardinalide/15809#15809

  • What’s in the result?

  • The result has a list of data of type GAR. If I make one .each with the result, the data is there. What I wanted was to pass this list to the function carregaGARsCriadas

  • @Ciganomorrisonmendez, I’ve been taking a look but I think it’s not what I want. I .getJson for the function /GAR/actualizarGARs I’m processing data, I just var listaGARTratadas = db.GAR.ToList(); to simplify the question

  • @Cesarmiguel Se is producing 5 objects with null values of properties, means that the ModelBinderis getting lost in creating the objects with the values. That’s why the BeginCollectionItem. He solves exactly this part.

  • @Ciganomorrisonmendez, I’m still not putting the data in partial. Soon in my function carregaGARsCriadas I check that the list of objects passed on .load wrong way. For example, instead of receiving an item with the data: ["idGAR: 12", "SerieGAR:19", etc etc] receiving ["idGAR: 0", "SerieGAR:null", etc etc]

  • I get it. I already tell you that there is no easy answer, because you need to serialize an object that has already been dehydrated.

  • Yes, it even makes sense that the object has been dehydrated

Show 3 more comments

2 answers

0

  • Marcelo, it is not recommended to use responses with links. Because nothing guarantees that the link will continue there with the content. The recommended is to put a summary of what is on the link and put the reference if someone wants to know more.

0


The solution I found to my question is to create an array of all the id’s 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 });

        });

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

 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");
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.