I have a page that lists the client, and I need to do a search by names, I have the method that does the research the problem and how to integrate it with the front end, looked for examples on the internet but I think my lack of notion let me understand no example.
Here is the method that lists clients in 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 is the method that would DO 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 done and was done by someone else who does not work here anymore, so I have a lot of problems.
Here is my page that shows the clients:
<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 difficult for someone to answer exactly what I want to be very specific, but if someone can give me a light, or an explanation of how these searches can be done, it would help me a lot, because neither research on the subject I'm getting it.
Thank you!