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.