Search using 2 types of info in the same text field!

Asked

Viewed 41 times

1

I’m trying a filter with Razor, search in the same text field, 2 information, you can type CNPJ or wallet, when I load the requests page, already loads with the data of CNPJ and Wallet on the screen, but I have a search button that when you type one of the two, for example CNPJ, the field Wallet comes empty, I managed to do this in Controller, but I would like to do it right in HTML Razor, let’s see the following rules:

Partialfiltro.cshtml

@model DPE.Programa.Dados.Models.tb_solicitacao
<div class="form-group col-md-2 ">
            <span>Cnpj/Carteira:<br>
            </span>
            @if (String.IsNullOrEmpty(Model.cnpj))
            {
                <input type="text" class="form-control" value="@Model.carteira"  id="carteira" name="carteira" aria-expanded="true" />
            }
            else
            {
                <input type="text" class="form-control" value="@Model.cnpj"  id="cnpj" name="cnpj" aria-expanded="true" />
            }
            @*<input type="text" class="form-control" value=" @if (String.IsNullOrEmpty(Model.cnpj)) { @Model.carteira  } else { @Model.cnpj } "  id="carteira" name="carteira" aria-expanded="true" />*@
</div>
<button class="pull-right btn btn-primary btn-labeled fa fa-search" type="button" id="btn-pesquisar">Pesquisar</button>

We can see that in what I did will always load data from the Wallet which is a number like 128272, there brings in the result.

Now the controller where he has commented that I was able to search for CNPJ when typing in the field, picked by the size of it.

public ActionResult FiltroPesquisa(tb_solicitacao filtros, int? pagina)
        {
            try
            {
                    return PartialView("PartialResultadoPesquisa", ListarDados(filtros, pagina));
            }
            catch (Exception ex)
            {
                ExibirMensagem("Ocorreu um erro ao tentar executar o filtro.<br/>Erro: " + ex.Message, ETipoMensagem.Erro, 99);
                return PartialView("_ControleMensagem");
            }
        }

        public List<tb_solicitacao> ListarDados(tb_solicitacao filtros, int? pagina)
        {
            _paginacao.PageNumber = pagina ?? 1;
            _paginacao.RowspPage = 10;

            using (DB_ConsultaDomicilioContext context = new DB_ConsultaDomicilioContext())
            {
                
                var model = context.tb_solicitacao.Include("tb_credenciamento_solicitacao").Include("tb_faturamento_solicitacao").Include("tb_faturamento_flex_solicitacao").AsQueryable(); 

                //var auxCarteira = String.Empty;

                //FILTROS
                if (!String.IsNullOrEmpty(filtros.cnpj))
                    model = model.Where(f => f.cnpj.ToUpper().Trim().Contains(filtros.cnpj.ToUpper().Trim()));

                //if (filtros.cnpj.Length < 9)
                //{
                //    auxCarteira = filtros.cnpj;
                //    filtros.carteira = auxCarteira;
                //    filtros.cnpj = "";

                if (!String.IsNullOrEmpty(filtros.carteira))
                    model = model.Where(f => f.carteira.ToUpper().Trim().Contains(filtros.carteira.ToUpper().Trim()));
                //}
                if (!String.IsNullOrEmpty(filtros.email_solicitante))
                    model = model.Where(f => f.email_solicitante.ToUpper().Trim().Contains(filtros.email_solicitante.ToUpper().Trim()));
                if (filtros.data_email != DateTime.MinValue && filtros.data_email != null)
                    model = model.Where(f => f.data_email.ToShortDateString() == filtros.data_email.ToShortDateString());
                if (filtros.data_cadastro != DateTime.MinValue && filtros.data_email != null)
                    model = model.Where(f => f.data_cadastro.ToShortDateString() == filtros.data_cadastro.ToShortDateString());
                //Filtro Status
                if (filtros.id_status != 0)
                    model = model.Where(f => f.id_status == filtros.id_status);

                model = model.OrderByDescending(f => f.data_cadastro).AsQueryable();

                _paginacao.TotalRegistros = model.Count();
                _listDados = model.Skip(_paginacao.PageNumber * _paginacao.RowspPage - _paginacao.RowspPage).Take(_paginacao.RowspPage).ToList();

                ViewBag.PaginacaoManual = _paginacao;
                return _listDados;
            }
        }

And finally the screen I modified to show the data when it is CNPJ or Wallet or shows loading everything.

<div class="table-responsive">
    @{
        
        if (Model != null && Model.Count > 0)
        {
            
        <table class="table table-striped table-hover table-vcenter">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th class="text-center">CNPJ</th>
                    <th class="text-center">Carteira</th>
                    <th class="text-center">Email</th>
                    <th class="text-center">Data Email</th>
                    <th class="text-center">Data Entrada</th>
                    <th class="text-center">Status</th>
                    <th class="text-center">Resposta</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    var Status = Utilitario.EnumDescription((Itau.DPE.ConsultaDomicilio.Dados.Enumeradores.EStatusSolicitacao)item.id_status);
                    var auxCnpj = item.cnpj;
                    var auxCarteira = item.carteira;
                    
                    <tr>
                        <td class="text-center">@item.id_solicitacao</td>
                        @if (auxCnpj != "")
                        {
                            <td class="text-center">@item.cnpj</td>
                            <td class="text-center"></td>
                        }
                        else if (auxCarteira != null)
                        {
                            <td class="text-center"></td>
                            <td class="text-center">@item.carteira</td>
                        }
                        else
                        {
                            <td class="text-center">@item.cnpj</td>
                            <td class="text-center">@item.carteira</td>   
                        }
                        <td class="text-center icon-email-solicitacao" data-details="@Html.Raw(item.mensagem_email.Replace("nbsp", "<br />"))" title="Detalhe" style="text-decoration:underline; cursor:pointer">
                            @item.email_solicitante</td>
                        <td class="text-center">@item.data_email</td>
                        <td class="text-center">@item.data_cadastro</td>
                        <td class="text-center">@Status</td>
                        <td class="text-center">
                            @if (Status == "Respondida")
                            {
                                <a href="#" class="icon-detalhe-solicitacao" id="@item.id_solicitacao">
                                    <span class="fa fa-search"></span>
                                </a>
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
                Html.RenderPartial("_Paginacao");
        }
        else
        {
        <table class="table table-striped">
            <tr class="text-center">
                <th class="text-center">Não houve resultados para a pesquisa. </th>
            </tr>

        </table>        
        }     
    }
</div>

Main focus is on the first screen, I’m trying to figure out a way to be able to search for any of the two, but just by wallet I got, someone has done this type of search or has any idea what I could change to work on ASP.NET, thanks guys.

  • I had difficulty understanding the expected behavior of the application. Could explain better what is not working properly or what exactly you want to improve?

1 answer

0


Good morning Personal,

I found an answer to my question in searching either by CNPJ or Wallet when typing in the text field, the goal is to load on the web screen information with these different searches, I made a filter below in my method, I hope it helps who needs it and I want to thank everyone who saw and who supported me, follows below:

 public List<tb_solicitacao> ListarDados(tb_solicitacao filtros, int? pagina)
 {
     using (DB_ConsultaDomicilioContext context = new DB_ConsultaDomicilioContext())
     {
         if (!String.IsNullOrEmpty(filtros.cnpj))
            {
                var auxCNPJCarteira = filtros.cnpj.Trim();
                model = model.Where(f => f.cnpj.Contains(auxCNPJCarteira) || f.carteira.Contains(auxCNPJCarteira));
            }
     }
 }

Sincerely yours truly, Leandro de Azevedo.

Browser other questions tagged

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