1
I have a page that lists client, and I need to do a name search, I have the method that does the problem research and how to integrate it with the front-end, I looked for examples on the internet but I think my lack of notion did not let me understand any example.
Here is the method that lists customers in the C#:
public ActionResult Index(int PaginaAtual){
ClienteModel clienteModel = new ClienteModel();
clienteModel.PaginaAtual = PaginaAtual;
int LojistaId = Convert.ToInt32(User.Identity.Name);
int[] lojaId = this.LojaServico.GetMany(l => l.LojistaId == LojistaId).Select(l => new int[] { l.LojaId, l.Clientes.Count}).FirstOrDefault();
int LojaId = lojaId[0];
if(lojaId != null){
List<ClienteLoja> clientes = this.ClienteLojaServico.GetMany(x => x.LojaId == LojaId).OrderByDescending(x => x.DataCadastro).Skip(PaginaAtual*10).Take(10).ToList();
clienteModel.Clientes = clientes;
clienteModel.qtdeClientes = lojaId[1];
//model.LojaId = loja.LojaId;
return View(clienteModel);
}else{
return RedirectToAction("Index", "Home", new { area = "" });
}
}
And this here is the method that would make the search :
public ActionResult AjaxHandler(JQueryDataTableParamModel param, string LojaId)
{
int idLoja = Convert.ToInt32(SystemCriptografia.ToDescriptografaQueryString(LojaId));
//int LojistaId = Convert.ToInt32(User.Identity.Name);
//var Loja = this.LojaServico.GetMany(l => l.LojaId == idLoja && l.LojistaId == LojistaId).Select(l => new { l.LojaId, l.Clientes }).FirstOrDefault();
var Loja = this.LojaServico.GetMany(l => l.LojaId == idLoja).Select(l => new { l.LojaId, l.Clientes }).FirstOrDefault();
IEnumerable<Cliente> totalClientes = new List<Cliente>();
if (Loja != null)
{
totalClientes = Loja.Clientes.Where(c => c.Cliente.Ativo == true && c.Tipo == (byte)enumTipoVinculo.APP).OrderBy(m => m.Cliente.Mensagens.Where(x => x.LojaId == idLoja && x.OrigemId == 0 && x.Visualizada == false).Select(p => p.DataEnvio)).Select(a => a.Cliente);
}
IEnumerable<Cliente> filtroCliente;
if (!string.IsNullOrEmpty(param.sSearch))
{
string busca = param.sSearch.ToLower();
filtroCliente = ClienteServico.GetAll().Where(x => x.Nome.Contains(busca) ||
x.Email.Contains(busca) || x.Telefone.Contains(busca));
}
else
{
filtroCliente = totalClientes;
}
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<Cliente, string> orderingFunction;
switch (sortColumnIndex)
{
case 1:
orderingFunction = (c => c.Email);
break;
case 2:
orderingFunction = (c => c.Telefone);
break;
default:
orderingFunction = (c => c.Nome);
break;
}
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filtroCliente = filtroCliente.OrderBy(orderingFunction);
else
filtroCliente = filtroCliente.OrderByDescending(orderingFunction);
var displayedClientes = filtroCliente.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedClientes select new[] { c.Nome, c.Email, c.Telefone, "perfilFbLink" };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = totalClientes.Count(),
iTotalDisplayRecords = totalClientes.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
}
This last method was already ready and was done by someone else who no longer works here, so I’m in a lot of trouble.
Here is my page showing the customers:
<div class="row">
<div class="col-xs-12">
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<div class="row">
<div class="col-sm-5">
</div>
<div class="mailbox-controls" style="margin-right:20px;margin-bottom:10px">
<div class="pull-right" >
@QntdeInicial.ToString() - @QntdeFinal.ToString()
<div class="btn-group">
<a id="previousPage" href="@Url.Action("Index", "Cliente", new {PaginaAtual = @Model.PaginaAtual -1 , Area="Lojista"})">
<i class="btn btn-default btn-sm fa fa-chevron-left"></i>
</a>
<a id="nextPage" href="@Url.Action("Index", "Cliente", new {PaginaAtual = @Model.PaginaAtual +1 , Area="Lojista"}) ">
<i class="btn btn-default btn-sm fa fa-chevron-right">
</i>
</a>
</div>
</div>
<!-- /.pull-right -->
</div>
</div>
<div class="dataTables_wrapper form-inline dt-bootstrap" id="example2_wrapper">
<div class="col-sm-12">
<table aria-describedby="example2_info" role="grid" id="example2" class="table table-bordered table-hover dataTable">
<thead>
<tr role="row">
<th aria-label="Rendering engine: activate to sort column descending" aria-sort="ascending" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting_asc">
</th>
<th aria-label="" aria-sort="ascending" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting_asc">Nome</th>
<th aria-label="" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting">Email</th>
<th aria-label="" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting">Cidade</th>
<th aria-label="" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting">Telefone</th>
<th aria-label="" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting">Avaliacão</th>
<th aria-label="" colspan="1" rowspan="1" aria-controls="example2" tabindex="0" class="sorting">Ações</th>
</tr>
</thead>
<tbody id="tabela-clientes">
@foreach (ClienteLoja cli in Model.Clientes)
{
<tr class="odd" role="row">
<td align="center" class="sorting_1">
<img src="/Content/imagens/principais/no-user.png" alt="Product Image" style="height:40px">
</td>
<td>@(!String.IsNullOrEmpty(cli.Cliente.Nome) ? cli.Cliente.Nome : "Usuario nao cadastrado")</td>
<td>@(!String.IsNullOrEmpty(cli.Cliente.Email) ? cli.Cliente.Email : "Email nao cadastrado")</td>
<td>@if (cli.Cliente.EnderecoResidencial != null) { cli.Cliente.EnderecoResidencial.Cidade.Nome.ToString(); } else { Write("--"); } </td>
<td>@(!String.IsNullOrEmpty(cli.Cliente.Telefone) ? cli.Cliente.Telefone : "Telefone nao cadastrado")</td>
<td>@cli.Avaliacao.ToString()</td>
<td align="left">
<a class="openMyModal btn btn-info" data-id='{"nome": "@cli.Cliente.Nome","email": "@cli.Cliente.Email","pessoaId": "@cli.Cliente.PessoaId", "telefone": "@cli.Cliente.Telefone","perfilFace":"@cli.Cliente.PerfilFace" }' title="Ver informacoes do usuario" href="#myModal" data-toggle="modal" data-parametro="" data-target="#modalCliente"><i class="fa fa-user openMyModal" data-widget=""></i></a>
<a class="btn btn-warning" href="@Url.Action("Conversa", "Mensagem", new {ClienteId = cli.ClienteId, Area="Lojista"})">
<i class=" fa fa-envelope" title="Ver conversa com este usuario"></i>
</a>
@if (!String.IsNullOrEmpty(cli.Cliente.PerfilFace))
{
if (cli.Cliente.PerfilFace != "Nulo")
{
<a target="_blank" [email protected] class="btn btn-social-icon btn-facebook">
<i class="fa fa-facebook">
</i>
</a>
}
}
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
I find it hard for anyone to be able to answer exactly what I want because I’m very specific, but if someone can shed some light on it, or an explanation of how these searches can be done, it would help me a lot, because I can’t even research the subject I’m getting.
Thank you very much!
You are using the plugin Datatable?
– Randrade
I realized that in the old method Datatable was used, so the method is that way, but in the old method it brought ALL clients at once, now this paginated straight. So I had the idea to do a search by name memso, with another method in the C# controller that brings this same page only that only the filter clients, would not have any update in real time, but would be more usable.
– William Cézar
Look this article. It explains how to do what you want and has the download code. However, the evening I work out a complete answer.
– Randrade
I’m taking a look at the article but will already give time to finish, if you can post an answer I thank you, tomorrow I will try to implement according to the article or your reply, anyway thank you very much for your help .
– William Cézar