redirecting C#/javascript pages

Asked

Viewed 507 times

2

I am with a question a little bit silly ( I think ), I have to make a filter for customer and need to do urgent, so I gave a "quick" solution to my problem, created a controler that does customer research but now with a filter, and returns to a page similar to the previous one only with the filter, the problem is 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 is 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 did a post method, which is probably wrong seeing that the bbackend method is ActionResult, He even gets in the way and finds what I need but he doesn’t redirect to the Pesquisar.cshtml and I don’t know how to do it. It seems like a very primary thing to me but I just can’t visualize how it’s going to work, if anyone can help me.

1 answer

2


You can do this in many ways:

Loading the View with ajax

You do not need to change your controller once 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);
         }
   });

Use a post

This will load a new View, the View named Search, 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)

Browser other questions tagged

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