Get the elements of a Grid.View with active filters

0
  • I'm a rookie and just a little more than the basics.
  • I have a Grid.View that shows me the records of a BBDD.
  • I have a "document browser" that shows me one by one all the documents of the database and allows me to navigate among them.
  • What I need is to limit the elements among which I want to navigate, that is, instead of browsing one by one through all the records, which can only be done among those that appear as a result of applying filters in the Grid. View.

What I want is that once a filter is applied, it is able to store in some way what the resulting elements are, in order to navigate only between those. For example, select only the records with the journal "002", and store them in a list, and then pass them to the controller of the individual view.

This is the grid code

    @Html.Grid(Model).Columns(columns =>
{
columns.Add(c => c.num_diarC)
        .Titled("Diario")
        .SetWidth(60);

columns.Add(c => c.num_movC)
        .Titled("Movimiento")
        .SetWidth(60);

columns.Add(c => c.mes_diarC)
        .Titled("Mes")
        .SetWidth(60);

columns.Add(c => c.anno_diarC)
        .Titled("Año")
        .SetWidth(60);

columns.Add(c => c.codigoC)
        .Titled("Código")
        .SetWidth(70);

columns.Add(c => c.empresaC)
        .Titled("Centro")
        .SetWidth(60);

columns.Add(c => c.num_factC)
        .Titled("Nº Factura")
        .SetWidth(60);

columns.Add(c => c.fec_factC).Format("{0:d}")
        .Titled("Fecha factura")
        .SetWidth(100);

columns.Add(c => c.imp_factC).Format("{0:n} €")
        .Titled("Importe")
        .SetWidth(60);

columns.Add(c => c.nombreC)
        .Titled("Empresa")
        .SetWidth(70);

columns.Add()
        .Encoded(false)
        .Sanitized(false)
        .SetWidth(200)
        .RenderValueAs(c => Acciones(c));

}).WithPaging(BibliotecaDocu.src.Constantes.DISPLAY_DEFAULT_PAGE_SIZE).Sortable().Filterable(true).WithMultipleFilters().SetLanguage("es").EmptyText("No hay datos que mostrar").Named("filtrado")

And this one of the controller:

        public ActionResult Buscar()
    {
        List<Compra> dbCompras = db.Compras.OrderByDescending(c => c.num_diar).ToList();
        List<ModelCompra> compras = new List<ModelCompra>();

        compras = new ModelCompra().getCompras(dbCompras);
        return View(compras);
    }

I've tried this:

    @{
    List<string> lista = new List<string>();
    foreach (BibliotecaDocu.Models.ModelCompra compras in Model)
    {
    lista.Add(compras.num_factC);
    }
}

But create a list with all the values because I'm referring to the model, and what I want is to make it to the Grid.

    
asked by J.Acero 03.03.2017 в 16:45
source

2 answers

0

In the test you have failed to implement the filter. Change it by adding it like this:

foreach (BibliotecaDocu.Models.ModelCompra compras in Model)
{
    if(compras.num_diarC == "002")//Si cumple tu condición lo añades
        lista.Add(compras.num_factC);
}

On the other hand if Model has some filtering method implemented, you could use it.

    
answered by 08.03.2017 в 12:58
-1

You should do it on the side of your server, where you make your query when sending a filter send it to your query and from your query filter and reload the grid now with the data that was filtered.

    
answered by 03.03.2017 в 16:47