-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:
After clicking on page 2 it goes back to paging without the search filter.