Я создал приложение ASP. NET MVC, которое имеет некоторые функции. Искать и фильтровать. Я бы хотел, чтобы при вводе имени или буквы они уже появлялись в результатах без нажатия кнопки отправки. А еще способ, чтобы после исследования все записи были показаны снова.
Я тоже использую плагин PagedList
My Action of Controller:
public ActionResult Index(string sortOn, string orderBy,string pSortOn, string keyword, int? page)
{
var Listar = metodosCli.Listar();
if (!page.HasValue)
{
page = 1; // set initial page value
if (string.IsNullOrWhiteSpace(orderBy) || orderBy.Equals("asc"))
{
orderBy = "desc";
}
else
{
orderBy = "asc";
}
}
// override the sort order if the previous sort order and current
if (!string.IsNullOrWhiteSpace(sortOn) && !sortOn.Equals(pSortOn,StringComparison.CurrentCultureIgnoreCase))
{
orderBy = "asc";
}
var list = Listar.AsQueryable();
switch (sortOn)
{
case "NomeCliente":
if (orderBy.Equals("desc"))
{
list = list.OrderByDescending(p => p.NomeCliente);
}
else
{
list = list.OrderBy(p => p.NomeCliente);
}
break;
case "NascimentoCliente":
if (orderBy.Equals("desc"))
{
list = list.OrderByDescending(p => p.NascCliente);
}
else
{
list = list.OrderBy(p => p.NascCliente);
}
break;
default:
list = list.OrderBy(p => p.CodCliente);
break;
}
if (!string.IsNullOrWhiteSpace(keyword))
{
list = list.Where(f => f.NomeCliente.StartsWith(keyword));
}
var finalList = list.ToPagedList(page.Value, recordsPerPage);
return View(finalList);
}
Мое мнение:
@using PagedList
@using PagedList.Mvc
@model IPagedList<GerenciamentoContas.Models.ClienteModel>
@{
ViewBag.Title = "Lista de clientes";
}
<p>
<a class="btnCadastrar" href="@Url.Action("CadastroCli","Home")"> Registrar novo cliente </a>
<a class="btnCadastrar" href="@Url.Action("ClientesVencidos","Home")"> Ver clientes com contas vencidas </a>
</p>
@Html.ActionLink("Filtrar por ordem alfabética", "Index", new { sortOn= "NomeCliente", orderBy = ViewBag.OrderBy, pSortOn = ViewBag.SortOn, keyword =
ViewBag.Keyword })
@Html.ActionLink("Filtrar por idade", "Index", new { sortOn= "NascimentoCliente", orderBy = ViewBag.OrderBy, pSortOn = ViewBag.SortOn, keyword =
ViewBag.Keyword })
@using (Html.BeginForm())
{
<p>
Pesquisar cliente:
<input type="text" name="keyword" required value="@ViewBag.Keyword" />
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
Código do cliente
</th>
<th>
Nome
</th>
<th>
CPF
</th>
<th>
Data de nascimento
</th>
<th>
Telefone
</th>
<th>
E-mail
</th>
<th>
Quantidades de boletos
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CodCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.NomeCliente) @Html.DisplayFor(modelItem => item.SobrenomeCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.CpfCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.NascCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.TelCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailCliente)
</td>
<td>
@Html.ActionLink("Alterar dados", "AlterarCli", new { id = item.CodCliente }) |
@Html.ActionLink("Ver contas", "ContasCli", new { id = item.CodCliente }) |
@Html.ActionLink("Excluir", "ExcluirCli", new { id = item.CodCliente })
</td>
</tr>
}
@if (Model.Count == 1) { <tr><td><b>
Mostrando @Model.Count registro de @Model.TotalItemCount </b> </td></tr>}
else if(Model.Count < 1)
{
<tr><td><b> Não há registros para mostrar </b></td></tr>
}
else
{
<tr><td><b>Mostrando @Model.Count registros de @Model.TotalItemCount</b></td></tr>
}
</table>
@{
if (Model.TotalItemCount != Model.Count)
{
<div class="row">
<div class="col-md-12">
Página @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de
@Model.PageCount
</div>
</div>
}
}
<div class="pagedList">
@Html.PagedListPager(Model, page => Url.Action("Index", new
{ page, sortOn = ViewBag.SortOn, orderBy = ViewBag.OrderBy, pSortOn =
ViewBag.SortOn, keyword = ViewBag.Keyword }),
PagedListRenderOptions.ClassicPlusFirstAndLast)
</div>