Reverse lookup after ajax call

after an asynchronous call using jquery, how can I return to a separate view?

this is my caller view:

<script type="text/javascript"> function Run() { $.ajax({ type: "POST", cache: false, url: "/Home/Run", data: $("#form_run").serializeArray(), dataType: "json" }); } </script> <form action="javascript:return true;" method="post" id="form_run"> <input type="text" id="nome" name="nome" /> <input type="text" id="cognome" name="cognome" /> <input type="submit" name="submit" value="Run" onclick="Run();" /> </form> 

this is my controller action:

  [AcceptVerbs(HttpVerbs.Post)] public ActionResult Run(string nome, string cognome) { return View("Result"); } 

can't display the result view. How?

+4
source share
3 answers

You cannot return View from an asynchronous call. The ajax request is designed to avoid a whole page loop.

You just need to return the POST if you want to return a new View .

Another option is to have a callback on ajax call successful and set window.location to a new View so that the page gets GET for the new View .

+6
source

Use a callback function that will redirect the user after returning AJAX.

 ajax(/*suff*/).success(callbackFuntion); 

And then in your callback

 function callbackFuntion(){ window.location = "www.google.com"; } 
+3
source

In general, an ASP.NET MVC worklfow view is associated with a controller. Therefore, if you want to change the view to display different HTML content in the browser based on the AJAX request, simply enter so that the URL of this request is redirected to another controller . Create this controller to return a new View(...) .

You can take a look at ASP.NET MVC Views Overview

If this is not what you are asking for, please specify.

0
source

Source: https://habr.com/ru/post/1478997/


All Articles