Document search screen with pagination and search filter with asp.net [closed]

1

I have the layout of a search screen, it has to show documents from my sqlserver database. To facilitate the search I put filter and as they are very documents it would be interesting to do pagination. I'm still learning; so if anyone has a good tutorial or post some code for me to study, I would appreciate it. I can only do the research in a basic way, like searching for certain fields in the database. If anyone can help, thank you right away.

    
asked by anonymous 27.12.2017 / 01:40

1 answer

0

I'll post the form I use for my ASP.NET MVC searches with EntityFramework.

I define a ViewModel with the parameter fields and a list with the search results, it is in this class that the paging information will also be.

public class BancosViewModel : PagedListViewModel<Banco>
{
    public BancosViewModel()
    {
        Pagina = 1;
        TamanhoPagina = 50;
    }

    [Display(Name = "Página")]
    public int Pagina { get; set; }

    [Display(Name = "Itens por página")]
    public int TamanhoPagina { get; set; }

    [JsonIgnore]
    public IPagedList<Banco> Resultados { get; set; }

    //TODO: adicionar filtros de pesquisa
    public string Descricao { get; set; }
}

The search is done in the Action of my Controller

public async Task<ActionResult> Indice(BancosViewModel viewModel)
{        
    var query = _db.Bancos.AsQueryable();

    //TODO: parâmetros de pesquisa
    if (!String.IsNullOrWhiteSpace(viewModel.Descricao))
    {
        var descricaos = viewModel.Descricao?.Split(' ');
        query = query.Where(a => descricaos.All(descricao => a.Descricao.Contains(descricao)));
    }

    viewModel.Resultados = await query.OrderBy(a => a.Descricao).ToPagedListAsync(viewModel.Pagina, viewModel.TamanhoPagina);

    return View("Indice", viewModel);
}

The ToPagedListAsync method is part of the PagedList.EntityFramework package

The View that demonstrates the search results is as follows

@using Syns.Web.Areas.Cadastros.ViewModels
@model BancosViewModel
@{
    ViewBag.Title = "Bancos";
}

<div class="panel panel-default">
    <div class="panel-heading">
        Pesquisa - @ViewBag.Title
    </div>
    <div class="panel-body">
        <div class="row form-group">
            <div class="col-lg-12">
                @using (Html.BeginForm("Indice", "Bancos"))
                {
                    <div class="row form-group">
                        <div class="col-md-4 col-xs-6">
                            @Html.LabelFor(model => model.Descricao, htmlAttributes: new { @class = "control-label" })
                            @Html.EditorFor(model => model.Descricao, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Descricao, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="btn-group">
                                <button type="submit" class="btn btn-default" id="submit" name="submit">Pesquisar</button>
                            </div>
                        </div>
                    </div>
                }

            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div id="pesquisa">
                    <table class="table-bordered table-striped table-condensed cf table-hover col-md-12">
                        <thead class="cf">
                            <tr>
                                <th class="col-lg-2 col-md-3 col-xs-12"></th>
                                <th>
                                    Número
                                </th>
                                <th>
                                    Descrição
                                </th>
                            </tr>
                        </thead>

                        <tbody>
                            @foreach (var item in Model.Resultados)
                            {
                                <tr>
                                    <td data-title="">
                                        <div class="btn-group">
                                            <a class="btn btn-default" id="editar" title="Editar" href="@Url.Action("Editar", new { id=item.BancoId})"><i class="fa fa-edit fa-align-left"></i></a>
                                            <a class="btn btn-default" id="detalhes" title="Detalhes" href="@Url.Action("Detalhes", new {id=item.BancoId})"><i class="fa fa-eye fa-align-right"></i></a>
                                            <a class="btn btn-default" id="Deletar" title="Excluir" href="@Url.Action("Excluir", new { id=item.BancoId })"><i class="fa fa-trash fa-align-right"></i></a>
                                        </div>
                                    </td>
                                    <td data-title="Numero">
                                        @item.Numero
                                    </td>
                                    <td data-title="Descricao">
                                        @item.Descricao
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

@Html.PagedListPager(Model.Resultados, page => Url.Action("Pesquisa",
    new BancosViewModel()
    {
        Pagina = page,
        Descricao = Model.Descricao
    }), PagedListRenderOptions.Classic)

Helper @Html.PagedListPager .

    
27.12.2017 / 11:19