Controller code is executed but nothing happens

Asked

Viewed 71 times

0

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

This is the code that calls a 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 the Home Index and in this controller everything happens as it should, however in the browser the page does not change, everything is the same.

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

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

How is it possible for code to be executed and nothing happens?

  • Missed the return of $.ajax! and also the method is wrong ... should return a json to screen! Although now I was in doubt what is your main purpose?

2 answers

1

You’re making some trouble there.

First, you are using Ajax to do a normal search. However, you are not doing anything with it. As your return is a RedirectToAction(), Ajax doesn’t know what to do.

For your problem, there are some simple solutions, among them only a redirec via javascript even (there is no need for this Ajax and this redirect), in this way:

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

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

Now, there’s the way I recommend it to be done, which is using a GET form instead of javascript. It would be 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 want to understand more about the subject, this question has a doubt very similar to its.

  • Thank you. I tried to use redirect via javascript but it’s not working either.. The GET form worked but in an incorrect way. The search term is added in the address bar, which is what I want, but since the form that collects the search data is in the navigation bar, like the one on facebook, what would happen to me is that if I was in a login view, for example, the search term is added at this address, so /Acount/Login? query=wordfind. I still can’t get it to always redirect me to the /home/index

0


This code snippet performs an asynchronous request to your Controller:

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

And for being an asynchronous requisition, you 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 treating 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 what you really want is to redirect your page according to the text typed in the element #txtTermoDePesquisa. If this is the case, update the question with the used HTML 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 controller method;

@using (Html.BeginForm("Pesquisar", "Pensamento", FormMethod.POST))
{
        <input value="" type="text" name="termoAPesquisar" id="txtTermoDePesquisa" />
        <input type="submit" value="Pesquisar"/>
}
  • Thanks for the help. I tried to add the callback Function as it is on top but the results are the same. The goal is to take the value entered in a text box that is in the navigation bar, similar facebook, and send to a controller. What I might be doing wrong ?

  • @Romeo, I recommend that you study a lot what ajax is, because you will understand why you are not updating the page. I updated the response with a form that performs a request for your method in the controller using the POST method, which I believe can help you

  • 1

    That’s what I did, I went to school, and I realized that what I wanted to do couldn’t be done using ajax. I still have a lot to learn because I didn’t know this method to post Forms to the controller. It’s already working, thank you very much.

  • Mark the answer that helped you as correct (below your points), or answer your own question to help those with the same question! Good luck with your studies, @Romeo

Browser other questions tagged

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