Receive a Model per parameter in a function

Asked

Viewed 542 times

1

I’m trying to get one Model in a function, which is a step view (although I don’t know if it’s possible) to then generate a list and generate a PDF.

My View:

    @model List<BDOleoTorres.Models.AutoDeclaracoesCertISCC>
    Clique <a href="@Url.Action("downloadListaISCC", "Alertas", new { modelISCC = Model })">aqui</a> para gerar lista para PDF

My controller:

public ActionResult downloadListaISCC(List<AutoDeclaracoesCertISCC> modelISCC)
{
    //Código para gerar PDF com o modelISCC recebido
    return PartialView("AlertaCertOUAPartial");
}

I get the modelISCC always without data and I do not know if what I am trying to do is possible, if not I will try to find another solution.

  • I think you should just keep the identifier of the object and not the whole object, it would be something much more "beautiful". And in downloadListaISCC consult the object to generate the PDF.

  • How do I store only the object identifier?

3 answers

1


Eventually I came up with a solution that maybe not the most effective is working well. What I am doing is: I call a javascript function; I will fetch the value of the filters; I call the function (controller) to generate the PDF, sending the data of the filters; I recalculate the list and I call the PDF.

That is to say: View:

Clique <a onclick="downloadListaAlertasISCC()" style="cursor:pointer">aqui</a> para gerar lista para PDF

Function downloadListaAlertasISCC in the Javascript:

function downloadListaAlertasISCC() {
   var dataFiltro = $("#FiltrarListContrato").val();
   //Outros filtros
   window.location.href = "/Alertas/downloadListaISCC?idForn=" + dataFiltro + "";

}

Controller:

public ActionResult downloadListaISCC(DateTime? DataFimFiltro)

    //Calculo o resultado do filtros (lista)
    //Gerar a lista para PDF
    //Retornar ficheiro PDF gerado
}

1

What do I get for getting one Model in a Action is you have the representation of each property of that Model ma view and when the request to Action is made the framework of ASP.NET MVC will effect the parser fields to an instance of Model.

As you have already commented, create each field you want to move from each item in the list on view so that you can receive a List<Model> easy to manipulate in Action is something painful for the environment. Especially if you have no way to predict the size that your lists can reach.

If your list receives values typed by the user and so you need to post all the Model, then imagining as in an order screen in which each product is filled by the user, maybe post item by item is painful.

If that’s not it, if you just pass the list to the View and then just need to know what the items on that list were when it was posted to Action, then you can really solve it differently, with a list of Id s.

You at the time of setting up the view can create something like:

@model List<BDOleoTorres.Models.AutoDeclaracoesCertISCC>
...
@foreach (var I=0; I<Model.Count(); I++)
{
    <input type="hidden" id="modelId_@I" name="modelId[@I]" />
}
@* Outros campos de forma que não precisam ser postados *@
....

In his Action you get so:

public ActionResult downloadListaISCC(int[] modelId)
{
    // recupera sua lista do banco de dados 
    // processa a lista
}

That way, you’ll surely be saving resources.

If that list is about exclusions and additions in View and then it will depend on what the user will do, so it won’t be right to try to define the index of the fields to render, but to set the indexes before posting.

Example:

@model List<BDOleoTorres.Models.AutoDeclaracoesCertISCC>
...
@foreach (var I=0; I<Model.Count(); I++)
{
    <input type="hidden" id="modelId" name="modelId" />
}
@* Outros campos de forma que não precisam ser postados *@
....

Before posting, you would do something like:

$("#seuForm").submit(function () {
    var idx = 0;
    $("#modelId", this).each(function () {
        var $self = $(this);
        $self.attr("id", $self.attr("id") + "_" + idx);
        $self.attr("name", $self.attr("name") + "[" + idx + "]");
        idx++;
    });
});

This is necessary because if there is a cut in the index sequence your list will not be passed to the Model in Action completely.

Following this idea, you can add up to more than one field, for each of them you create a parameter as vector in Action, or you can opt for a ModelView, so it would look more "elegant".

0

Maybe it is more interesting to save the list of identifiers.

Something like that:

@model List<long>
    Clique <a href="@Url.Action("downloadListaISCC", "Alertas", new { ids = Model })">aqui</a> para gerar lista para PDF

And on the controller so:

public ActionResult downloadListaISCC(long[] ids)
{
    // Buscar no banco de dados pelo identificador
    //Código para gerar PDF com o modelISCC recebido
    return PartialView("AlertaCertOUAPartial");
}

Note: I have not tested, any problem comments.

Edit:

Take a look in that reference, I think it will help you.

  • the problem is I have a List and not just one element, so this solution will not work

  • There is a filter to load this list?

  • Yes there is. I really have to send the Model I’m receiving in the view

  • So you have to save, only what is necessary to retrieve the list, why serializing a gigantic list in the customer can cost expensive.

  • I edited my answer, but I think your problem is with the list, I think it won’t even work like this.

  • Yeah, that’s not gonna work because I really need that kind of data (AutoDeclaracoesCertISCC) because I’m printing a list. What I can try to do is send the id’s in a list by a viewbag

  • Maybe you have to use a more primitive kind of list like ICollection or [], in a case here use [] to a list of ids (long[] ids), but the request is made by Ajax, passing an object(array) JSON.

  • I ended up going by Javascript. Send the filters from js, then in the controller I recalculate the list (with the result of the filters) and then send the PDF. Maybe it’s not the most optimized way, because I’m calculating twice the same list, but patience will stay that way. Thanks for the help!

  • I only know that it is not a good practice to store large amount of information in the client(browser) or in Session(server memory), in the case of MVC who came to correct the good and heavy WebForms. And if you can post your solution to other users who have the same problem.

Show 4 more comments

Browser other questions tagged

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