从相同的动作返回重定向或PartialView [英] Returning Redirect or PartialView from the same Action

查看:204
本文介绍了从相同的动作返回重定向或PartialView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Ajax.BeginForm问题。我有一个提交按钮和一个更新局部视图的形式。

I have a problem with Ajax.BeginForm. I have a form with submit and button that updates partial view.

我试图做到的是,我可以用在同一个动作以下return语句。

What I am trying to accomplish is that I could use following return statements in the same Action.

// button activates this
return PartialView("PartialViewName", data);

// submit activates this
return Redirect(model.Url);

现在的问题是,重定向导致问题的Ajax.BeginForm

Now the problem is that Redirect causes problems to the Ajax.BeginForm

<% using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { OnSuccess = "OnSuccess", UpdateTargetId = "Container", InsertionMode = InsertionMode.Replace }, new { id = "eventAjaxForm" })) { %>

如何才能辨别是结果PartialView,还是我们重定向用户?

How can I identify is the result PartialView or are we redirecting user?

推荐答案

通常情况下,当你执行一个AJAX调用到一个控制器行动的任何重定向跟着,直到客户得到200状态code或错误。所以,你重定向到URL的HTML将被放置在集装箱 DIV。在你的控制器动作另一方面,你可以 Request.IsAjaxRequest()测试电话是否使用异步和返回部分。

Normally when you perform an AJAX call to a controller action any redirects are followed until the client gets 200 status code or an error. So the HTML of the url you are redirecting to will be placed in the Container div. On the other hand in your controller action you could test whether the call was asynchronous using Request.IsAjaxRequest() and return a partial.

这篇关于从相同的动作返回重定向或PartialView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆