Driver code runs but nothing happens

0

After much research without results I come here to question you. The project is in MVC 5 using C# .

This is the code that calls an MVC controller

$('#btnPesquisa').click(function () {
    var textAPesquisar = $("#txtTermoDePesquisa").val();
    $.ajax({
           url: "/Pensamento/Pesquisar",
           type: 'POST',
           contentType: "application/json; charset=utf-8",
           data: '{termoAPesquisar:' + JSON.stringify(textAPesquisar) + '}',
           async: false
    });
});

When I debug I see that the controller receives the value and even redirects me to Home Index and in this controller everything happens as it should, however in the browser the page does not change, everything stays the same.

[HttpPost]
public ActionResult Pesquisar(string termoAPesquisar)
{
   return RedirectToAction("Index", "Home", new {query = termoAPesquisar });
}    

If I call Home Index manually, through the browser address bar, everything happens as it should.

How can the code be executed and nothing happen?

    
asked by anonymous 05.01.2017 / 12:24

2 answers

0

This code snippet performs an asynchronous request to its Controller :

 $.ajax({
           url: "/Pensamento/Pesquisar",
           type: 'POST',
           contentType: "application/json; charset=utf-8",
           data: '{termoAPesquisar:' + JSON.stringify(textAPesquisar) + '}',
           async: false
    });

And because it is an asynchronous request, its Controller processes this request but you are not doing anything with its result. For this, you need to add to the code the callback responsible for handling this return:

$.ajax({
           url: "/Pensamento/Pesquisar",
           type: 'POST',
           contentType: "application/json; charset=utf-8",
           data: '{termoAPesquisar:' + JSON.stringify(textAPesquisar) + '}',
           async: false,

           success: function(data){
               console.log(data);
           }
    });

However, I believe that what you really want is to redirect your page according to the text entered in the #txtTermoDePesquisa element. If this is the case, update the question with the HTML used that I update the answer with what you need for it to happen without using ajax.

Below is a way to post a form to your method on the controller;

@using (Html.BeginForm("Pesquisar", "Pensamento", FormMethod.POST))
{
        <input value="" type="text" name="termoAPesquisar" id="txtTermoDePesquisa" />
        <input type="submit" value="Pesquisar"/>
}
    
05.01.2017 / 13:43
1

You're making some confusion here.

First, you're using Ajax to do a normal hunt. But you're not doing anything about it. Since your return is RedirectToAction() , Ajax does not know what to do.

For your problem, there are some simple solutions, among them just a redirect via javascript itself (there is no need for this Ajax and this redirect), like this:

$('#btnPesquisa').click(function () {
    var textAPesquisar = $("#txtTermoDePesquisa").val();
    window.location.href = '@Url.Action("Index", "Home")?query=' + textAPesquisar;
});

Note that your POST for /Pensamento/Pesquisar no longer exists. That's because you do nothing but Redirect, so I do not see the need for it.

Now, it has the form that I recommend to be done, which is using a GET form instead of javascript. It would look something like this:

@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { @class = "search" }))
{
    <div>
        <input name="query" value="" type="search" id="txtTermoDePesquisa"/>
        <input type="submit" value="Pesquisar" id="btnPesquisa"/>
    </div>
}

If you'd like to understand more about it, this question has a question very similar to yours.

    
05.01.2017 / 14:04