栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过敲出js中的Ajax jQuery调用的MVC RedirectToAction无法正常工作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过敲出js中的Ajax jQuery调用的MVC RedirectToAction无法正常工作

由于您使用AJAX调用

RedirectToManageAccount
action方法,因此您有责任自己处理它的响应,并且由于
success
处理函数为空,因此您实际上将忽略作为响应到达的任何内容。

如果您想从AJAX响应处理程序中强制重定向,建议

  1. 修改操作方法如下
        [HttpPost]    public ActionResult RedirectToManageAccount(string accountNumber)    {        var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index2", "ManageAccount", new { companyId = "7e96b930-a786-44dd-8576-052ce608e38f" });        return Json(new { Url = redirectUrl });    }
  1. 以这种方式更新您的AJAX呼叫
        self.redirectToManageAccount = function () {      var accountNumber = "7e96b930-a786-44dd-8576-052ce608e38f";      $.ajax({ type: "POST",    url: "/SetUpNewCompany/RedirectToManageAccount",    data: { accountNumber: accountNumber },    dataType: 'json',    success: function (response) {        window.location.href = response.Url;    },    error: function () {    }      });    }

至于第二个问题:

另外,如何获取下面的URL,而不是带有查询字符串的URL

http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f

您只需在

RegisterRoutes()
函数中为此URL定义适当的路由条目:

    routes.MapRoute(null,         "ManageAccount/Index2/{companyId}",         new { controller = "ManageAccount",    action = "Index2" }          );

编辑 :由于您的AJAX调用仅用于调用导致重定向的操作方法,因此您可以按照以下方式进行简化,只要此时(在客户端)您

companyId
已经知道:

    self.redirectToManageAccount = function () {        var companyId = "12345";        window.location.href = '@(Html.ActionUri("Index2", "ManageAccount"))?companyId=' + companyId;    }

我在哪里使用这种扩展方法

    public static string ActionUri(this HtmlHelper html, string action, string controller)    {        return new UrlHelper(html.ViewContext.RequestContext).Action(action, controller);    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406682.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号