user2336491
user2336491

Reputation: 47

return view after ajax call

after an async call with jquery how can I return a particolar view?

this 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 my controller action:

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

can not display view "Result" How?

Upvotes: 4

Views: 15785

Answers (3)

Tigran
Tigran

Reputation: 62265

In common ASP.NET MVC worklfow a view is associated to a controller. So if you want to change a view, to render a different HTML content in the browser, based on AJAX request, just make in a way that url of that request is routed to another controller. Form that controller return a new View(...).

Can have a look on ASP.NET MVC Views Overview

If this is not what you're asking for, please clarify.

Upvotes: 0

Ryan Erb
Ryan Erb

Reputation: 838

Use a call back function that will redirect the user once the AJAX returned.

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

And then in your callback

function callbackFuntion(){

    window.location = "www.google.com";
}

Upvotes: 3

Gabe
Gabe

Reputation: 50523

You can not return a View from an asynchronous call. An ajax request is meant to avoid doing an entire page cycle.

You should just do a POST back if you want a new View to be returned.

The other option would to be have a callback upon success of the ajax call and set the window.location to the new View so the page does a GET to the new View.

Upvotes: 7

Related Questions