Receive one Model per parameter in a function

1

I'm trying to get a Model in a function, which passes from a view (although I do not know if it's possible) to be able to 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 modelISCC always with no data and I do not know if what I'm trying to do is possible, if not I'll try to find another solution.

    
asked by anonymous 26.03.2014 / 12:21

3 answers

1

I ended up getting a solution that maybe not being the most effective is working well. What I'm doing is: I call a javascript function; I'll get the value of Filters; I call the function (controller) to generate the PDF, sending the data of the filters; I re-compose the list and generate the PDF.

That is: View:

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

Function downloadListaAlertasISCC in 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
}
    
26.03.2014 / 13:53
1

What I understand by receiving a Model in a Action is that you have the representation of each property of that Model ma view and when the request to Action is made the framework ASP.NET MVC will parse the parser fields for an instance of Model .

As you've already commented, creating each field that you want to traffic from each list item in view so that you can receive an List<Model> easy to handle Action is somewhat painful for the environment. Especially if you can not predict what size your lists can reach.

If your list receives values typed by the user and therefore you need to post all Model , then wondering how in a request screen where each product is populated by the user, maybe posting item to item is less painful

If it is not, if you just pass the list to View and then just need to know what the items in that list were when it is posted to Action , then you can actually solve it in another way, with a list of Id's.

At the time of mounting the view you 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 your Action you get:

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

That way you're sure to be saving money.

If this list is about exclusions and additions in View and then it will depend on what the user is doing, then it will not work to set the index of the fields to render, but rather to index 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 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 a vector in Action , or you can opt for a ModelView , so it would look more "elegant". >     

29.03.2014 / 16:14
0

It may be more interesting to save the list of identifiers.

Something like this:

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

And in the controller like this:

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 did not test, no problem comments.

Edit:

Take a look this reference, I think will help you.

    
26.03.2014 / 12:44