如何获得 ASP.NET MVC Ajax 响应以重定向到新页面而不是将视图插入到 UpdateTargetId 中? [英] How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId?

查看:13
本文介绍了如何获得 ASP.NET MVC Ajax 响应以重定向到新页面而不是将视图插入到 UpdateTargetId 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ajax.BeginForm 创建一个表单,它将对某个控制器操作执行 ajax 回发,然后如果操作成功,用户应该被重定向到另一个页面(如果操作失败,则显示状态消息使用 AjaxOptions UpdateTargetId 显示).

I am using the Ajax.BeginForm to create a form the will do an ajax postback to a certain controller action and then if the action is successful, the user should get redirected to another page (if the action fails then a status message gets displayed using the AjaxOptions UpdateTargetId).

using (Ajax.BeginForm("Delete", null,
        new { userId = Model.UserId },
        new AjaxOptions { UpdateTargetId = "UserForm", LoadingElementId = "DeletingDiv" },
        new { name = "DeleteForm", id = "DeleteForm" }))
   {
    [HTML DELETE BUTTON]
   }

如果删除成功,我将返回重定向结果:

If the delete is successful I am returning a Redirect result:

[Authorize]
public ActionResult Delete(Int32 UserId)
{
    UserRepository.DeleteUser(UserId);
    return Redirect(Url.Action("Index", "Home"));
}

但是 Home Controller Index 视图正在加载到 UpdateTargetId 中,因此我最终得到了一个页面中的一个页面.我在想两件事:

But the Home Controller Index view is getting loaded into the UpdateTargetId and therefore I end up with a page within a page. Two things I am thinking about:

  1. 要么是我在设计这个错误,应该处理这种类型的操作不同(不使用 ajax).
  2. 而不是返回重定向结果,返回一个具有javascript 在其中执行在客户端重定向.

有人对#1 有意见吗?或者,如果 #2 是一个很好的解决方案,那么重定向 javascript 视图"会是什么样子?

Does anyone have comments on #1? Or if #2 is a good solution, what would the "redirect javascript view" look like?

推荐答案

您可以使用 JavascriptResult 来实现这一点.

You can use JavascriptResult to achieve this.

重定向:

return JavaScript("window.location = 'http://www.google.co.uk'");

重新加载当前页面:

return JavaScript("location.reload(true)");

似乎是最简单的选择.

这篇关于如何获得 ASP.NET MVC Ajax 响应以重定向到新页面而不是将视图插入到 UpdateTargetId 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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