ASP.NET MVC3:使用 AJAX 发布帖子时如何重定向到视图?

发布于 2024-10-31 10:08:00 字数 954 浏览 0 评论 0原文

我有一个搜索视图,它使用 AJAX 向服务器发送请求。根据存储库返回的响应对象,应在搜索视图(发送请求的视图)上显示一个对话框(使用 Json 对象填充数据),或者应将用户重定向到结果视图(填充来自作为视图模型传递的响应的数据)。

现在我被告知(并且有经验),当使用 AJAX 发布帖子时,无法重定向。那么,如果从存储库获取响应,是否有某种方法可以重定向到另一个视图并传递视图模型,并且如果应该显示对话框结果,则只需回发 Json 对象。

我的控制器操作由搜索视图发布,目前看起来像这样:

    [HttpPost]
    public ActionResult SomeAction(SearchRequest reqData)
    {
        ResponseBase response = worker.PerformSearch(reqData);

        if (response is ViewResponse)
        {
            //Redirect to "AnotherView" and pass response as the view model.
            return View("AnotherView", response as ViewResponse);
        }
        else if (response is DialogResponse)
        {
            //Return the Json object.
            return Json(new { type = "dialogresponse", data = response });
        }
        else
        {
            //To do: Put error handling code here.
            throw new NotImplementedException();
        }
    }

I have a search view which uses AJAX to send a request to the server. Depending on the response object returned by the repository, a dialog should be shown (populated with data using a Json object) on the search view (the view from which the request was sent) or the user should be redirected to a results view (populated with data from the response passed as a view model).

Now I have been told (and experienced) that one cannot redirect when a post was made using AJAX. So is there some way to redirect to another view and pass the view model if that response was obtained from the repository and to just post back the Json object if the dialog result should be shown.

My controller action that gets posted to by the search view currently looks somethings like this:

    [HttpPost]
    public ActionResult SomeAction(SearchRequest reqData)
    {
        ResponseBase response = worker.PerformSearch(reqData);

        if (response is ViewResponse)
        {
            //Redirect to "AnotherView" and pass response as the view model.
            return View("AnotherView", response as ViewResponse);
        }
        else if (response is DialogResponse)
        {
            //Return the Json object.
            return Json(new { type = "dialogresponse", data = response });
        }
        else
        {
            //To do: Put error handling code here.
            throw new NotImplementedException();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2024-11-07 10:08:00

这个问题有点老了,但我想我会提供我的意见。
我遇到过类似的情况,我必须发表帖子,然后用更新内容填充页面上的一些 html,或者将用户重定向到新位置。
可能有更好的方法来实现这一点,但这就是我所做的。
如果您使用 jQuery 来执行发布,它会足够智能地查看响应的内容类型;要返回视图,只需像现在一样,并在成功函数中填充内容。如果您希望重定向用户,

        string javascript = "window.location.href='{0}';";
        return JavaScript(string.Format(javascript, returnUrl));

mvc 框架会将 javascript mime 类型附加到响应中,jQuery 将选择该类型并执行 javascript,然后重定向用户。
唯一的问题是 JavaScript 将被填充到成功方法中的 html 内容中。要克服这个问题,请询问 html 的内容类型,类似于此处

This question is a bit old, but I thought I would offer my input.
I had a similar situation where I had to make a post and either populate some html on the page with update content, or redirect the user to a new location.
There are probably better ways to accomplish this however, this is what I did.
If you are using jQuery to perform the post it is smart enough to look at the response's content type; to return a view just do as you are now and in the success function populate the content. If you wish to redirect the user

        string javascript = "window.location.href='{0}';";
        return JavaScript(string.Format(javascript, returnUrl));

The mvc framework will attach the javascript mime type to the response, jQuery will pick that up and execute the javascript, and redirect the user.
The only catch is the javascript will be populated into the html content in your success method. To overcome this interrogate the content type for html, simmilar to here

诗笺 2024-11-07 10:08:00

我建议您将其分成两个电话。在第一个 AJAX 调用中检查它是 ViewResponse 还是 DialogResponse。如果 ViewResponse 进行另一个 AJAX 调用来获取实际结果对象,否则进行非 AJAX 服务器调用以重定向到新视图。

I would suggest that you split it into two calls. In the first AJAX call check if it is a ViewResponse or a DialogResponse. If ViewResponse make another AJAX call to get the actual result object else make a non-AJAX server call to redirect to the new view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文