Pagination with search

Asked

Viewed 204 times

-1

I am trying to create a search in a grid with paging, but when it brings the search result it generates a new pagination but when I click on page 2 it brings all the results ignoring the search and do not know how to solve.

 public class ListarController : Controller
{
    private ApplicationContext context;

    public ListarController(ApplicationContext context)
    {
        this.context = context;
    }
    public IActionResult Listar(string cpf, string ultimoNome,int pagina = 1)
    {
        if (cpf != null && ultimoNome == null)
        {
            var list = context.Usuario.Where(m => m.CPF == cpf && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else if (cpf == null && ultimoNome != null)
        {
            var list = context.Usuario.Where(m => m.UltimoNome == ultimoNome && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else if (cpf != null && ultimoNome != null)
        {
            var list = context.Usuario.Where(m => m.UltimoNome == ultimoNome && m.CPF == cpf && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else
        {
            var list = context.Usuario.Where(m => m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
    }        
    public IActionResult Editar(int id)
    {
        var usuario = context.Usuario.SingleOrDefault(m => m.Id == id);          

        return View(usuario);
    }
    [HttpPost]
    public IActionResult Editar(Usuario user)
    {

        context.Update(user);
        context.SaveChanges();
        return RedirectToAction("Listar");
    }
    public IActionResult Deletar (int id)
    {
        var usuario = context.Usuario.Find(id);
        usuario.Status = "D";
        context.SaveChanges();
        return RedirectToAction("Listar");
    }
}    

cshtml

<nav class="navbar navbar-inverse navbar-fixed-top" id="menu">
    <div class="container">
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li><a asp-area="" asp-controller="Crud" asp-action="Index">Home</a></li>
                <li><a asp-area="" asp-controller="Usuario" asp-action="Cadastrar">Cadastrar</a></li>
                <li><a asp-area="" asp-controller="Crud" asp-action="Listar">Listar</a></li>
            </ul>
        </div>
    </div>
</nav>




<div class="container">


    <form action="Listar" method="post" >

        <div class="form-row">
            <div class="form-group col-md-6">
                <label>Cpf:</label>
                <input type="text" name="cpf" id="cpf" class="form-control" placeholder="Cpf" />
            </div>

            <div class="form-group col-md-6">
                <label>UltimoNome:</label>
                <input type="text" name="ultimoNome" class="form-control" placeholder="Ultimo Nome" />
            </div>
            <div class="form-group col-md-7">
                <button type="submit" class="btn btn-primary" value="Listar">
                    Procurar
                </button>
            </div>
        </div>
    </form>




    <table class="table">
        <tr>
            <th>
                @Html.DisplayName("Id")
            </th>
            <th>
                @Html.DisplayName("Nome")
            </th>
            <th>
                @Html.DisplayName("UltimoNome")
            </th>
            <th>
                @Html.DisplayName("CPF")
            </th>
            <th>
                @Html.DisplayName("Nascimento")
            </th>
            <th>
                @Html.DisplayName("Sexo")
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelTem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelTem => item.Nome)
                </td>
                <td>
                    @Html.DisplayFor(modelTem => item.UltimoNome)
                </td>
                <td>
                    @Html.DisplayFor(modelTem => item.CPF)
                </td>
                <td>
                    @Html.DisplayFor(modelTem => item.Nascimento)
                </td>
                <td>
                    @Html.DisplayFor(modelTem => item.Sexo)
                </td>
                <td>
                    <a asp-action="Editar" asp-route-id="@item.Id">Editar</a>
                    <a asp-action="Deletar" asp-route-id="@item.Id">Deletar</a>
                </td>
            </tr>
        }           

    </table>       

    <div class="row">
        <div class="col-md-8">
            @Html.PagedListPager(Model, pagina => Url.Action("Listar", new { pagina }))
        </div>
        <div class="col-md-4">
            <span class="pull-right">
                Listando registros de @Model.FirstItemOnPage a @Model.LastItemOnPage de um total de @Model.TotalItemCount
            </span>
        </div>

    </div>

</div>

Filtered result:

inserir a descrição da imagem aqui

After clicking on page 2 it goes back to paging without the search filter.

inserir a descrição da imagem aqui

1 answer

0


You missed adding filters to the paging link. In your view, in Url.Action("Listar", new { pagina })), add CPF filters and last name.

Url.Action("Listar", new { pagina,  cpf = ViewBag.Cpf, ultimoNome = ViewBag.ultimoNome}))

For them to work, you still need to change the controller by adding

ViewBag.Cpf = cpf;
ViewBag.ultimoNome = ultimoNome;
ViewBag.pagina = pagina;

For easy reading and maintenance of your controller, also change your code block

if (cpf != null && ultimoNome == null)
        {
            var list = context.Usuario.Where(m => m.CPF == cpf && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else if (cpf == null && ultimoNome != null)
        {
            var list = context.Usuario.Where(m => m.UltimoNome == ultimoNome && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else if (cpf != null && ultimoNome != null)
        {
            var list = context.Usuario.Where(m => m.UltimoNome == ultimoNome && m.CPF == cpf && m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }
        else
        {
            var list = context.Usuario.Where(m => m.Status != "D").ToPagedList(pagina, 5);
            return View(list);
        }

For something like

        var list = context.Usuario.Where(m => m.Status != "D");
        if (!string.IsNullOrEmpty(cpf))
        {
            list = list.Where(m => m.CPF == cpf);
        }
        If (!string.IsNullOrEmpty(ultimoNome))
        {
            list = list.Where(m => m.UltimoNome == ultimoNome);
        }
        return View(list.ToPagedList(pagina, 5));

PS: the code was made on mobile and may contain typos or spell checker.

Browser other questions tagged

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