redirecting C # / javascript pages

2

I have a somewhat silly question (I think), I have to do a filter for client and I need to do it urgently, so I gave a "fast" solution to my problem, I created a controler that does client search but now with a filter, and returns to a page similar to the previous one only with the filter, the problem and that my page is not redirecting correctly.

So this my search button:

<div class="input-group input-group-sm" style="width: 200px;">
            <input name="ParametroBuscar" id="ParametroBuscar" class="form-control pull-right" placeholder="Pesquisar" type="text">
            <div class="input-group-btn">
                    <button type="submit" id="btnPesquisar" class="btn btn-default"><i class="fa fa-search"></i></button>
                </div>
        </div>

My backend looks like this:

public ActionResult Pesquisar(string ParametroBuscar)
    {
        int LojistaId = Convert.ToInt32(User.Identity.Name);
        int lojaId = this.lojaServico.GetMany(l => l.LojistaId == LojistaId).Select(l =>  l.LojaId).FirstOrDefault();




        ClienteModel clienteModel = new ClienteModel();
          if (!string.IsNullOrEmpty(ParametroBuscar)){

              List<ClienteLoja> clientes = ClienteLojaServico.GetMany(l => l.LojaId == lojaId && l.Cliente.Nome != null && l.Cliente.Nome != "" && l.Cliente.Nome.Contains(ParametroBuscar)).ToList();
              clienteModel.Clientes = clientes;
              clienteModel.ParametroBusca = ParametroBuscar;
              clienteModel.LojaId = lojaId;
          }



        return View(clienteModel);
    }

And I created a javascript like this:

  function pesquisarCliente() {
    var parametroPesquisa = {
        'ParametroBuscar': $('#ParametroBuscar').val()
    };
    // lembrando que 'exemplo' será o nome da variavel recebida como parametro, no C#
    $("#disableModal").addClass("disabledbutton");
    $('#loadingModal').show();
    $("#Conteudo").prop('disabled', true);
    $("#btnSubmit").prop('disabled', true);
    $.ajax({
        url: "/Cliente/Pesquisar", //altere para o caminho que você vai querer mandar o post
        type: "get", //tipo post
        data: parametroPesquisa, //os dados
        success: function (json) {
            if (json.isRedirect) {
                window.location.href = '@Url.Action("Cliente", "Pesquisa", new { ParametroBuscar = "__ParametroBuscar__" })';
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //se deu erro
            console.log(textStatus, errorThrown);

        }
    });
}

I made a post method, which is probably wrong seeing that the bbackend method is ActionResult , it even goes into the method and finds what I need but it does not redirect to Pesquisar.cshtml and I do not know how to do it . It seems like a very primal thing to me but I just can not visualize how it works, if anyone can help me.

    
asked by anonymous 27.09.2016 / 18:47

1 answer

2

You can do this in several ways:

Loading the View with ajax

You do not need to change your controller since it will return a View. Change the ajax call to load the View with the results inside a div or html element:

   $.ajax({
         type: "GET",
         url: '@Url.Action("Pesquisar", "Cliente")',
         data : { ParametroBuscar : $('#ParametroBuscar').val() }
         success: function (html) {
                $('#algumaDiv').html(html);
         }
   });

Using a post

This will load a new View, the View with Search name, since you did not specify the name.

@using(Html.BeginForm("Pesquisar", "Cliente")) 
{
  <div class="input-group input-group-sm" style="width: 200px;">
     <input name="ParametroBuscar" id="ParametroBuscar" class="form-control pull-right" placeholder="Pesquisar" type="text">
     <div class="input-group-btn">
           <button type="submit" id="btnPesquisar" class="btn btn-default"><i class="fa fa-search"></i></button>
      </div>
 </div>
}

Change to HttpPost

[HttpPost]
public ActionResult Pesquisar(string ParametroBuscar)
    
27.09.2016 / 20:41