return view after ajax call

不问归期 提交于 2019-12-21 17:48:08

问题


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?


回答1:


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.




回答2:


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";
}



回答3:


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.



来源:https://stackoverflow.com/questions/16360820/return-view-after-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!