【问题标题】:MVC RedirectToAction through ajax jQuery call in knockoutjs is not working通过敲除js中的ajax jQuery调用的MVC RedirectToAction不起作用
【发布时间】:2012-08-13 21:52:53
【问题描述】:

我正在构建一个 MVC3 Web 应用程序,并且正在使用 knockoutjs。应用程序中有两个视图。 SetUpNewCompany 和 ManageAccount。要设置新公司,用户首先输入帐号并单击搜索。如果帐号已经存在,用户可以点击按钮进入 ManageAccount 视图。在 SetUpNewCompanyController 中,我使用 RedirectToAction 方法进行重定向。但是,执行 ManageAccount 中的 Index2 操作时,不会显示视图。如果我输入完整的 URL,就会显示视图。

SetUpNewCompanyController.cs

[HttpPost]
 public RedirectToRouteResult RedirectToManageAccount(string accountNumber)
 {
        return RedirectToAction("Index2", new RouteValueDictionary(new {controller=
            "ManageAccount", companyId = "7e96b930-a786-44dd-8576-052ce608e38f" }));
 }

当点击按钮时,上面的函数会被下面的函数调用

self.redirectToManageAccount = function () {
        var accountNumber = "7e96b930-a786-44dd-8576-052ce608e38f";
        $.ajax({
            type: "POST",
            url: "/SetUpNewCompany/RedirectToManageAccount",
            data: { accountNumber: accountNumber },
            success: function (data) {
            },
            error: function () {
            }
        });
 }

ManageAccountController.cs

public ActionResult Index2(String companyId)
    {
        var viewModel = new Models.Index();

        List<String> compList = new List<String>();
        compList.Add("MyCompany");

        List<String> usersList = new List<String>();
        usersList.Add("User1");

        viewModel.Users = usersList;
        viewModel.Companies = compList;
        viewModel.CompanyId = companyId;
        viewModel.Role = "Role1";

        return View("ManageAccount",viewModel);
    }

生成的网址是

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

Firebug 中的控制台窗口显示

GET http://localhost:53897/ManageAccount/Index2?companyId=7e96b930-a786-44dd-8576-
052ce608e38f 200 OK and the spinner keeps spinng

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

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

【问题讨论】:

    标签: ajax asp.net-mvc-3 knockout.js


    【解决方案1】:

    由于您使用 AJAX 调用 RedirectToManageAccount 操作方法,因此您有责任自己处理其响应,并且由于您的 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 });
      }
      
    2. 以这种方式更新您的 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);
    }
    

    【讨论】:

    • 是否有另一种方法可以重定向到 ManageAccount/Index2 我也按照您的建议添加了路线,但我仍然在 URL 中得到 ?companyId=xxxxx
    • 这取决于具体的场景,真的。我用重定向过程的一种可能简化来更新我的答案。至于路线:你确定你没有在一些更一般的规则之后添加它吗?
    • 我把 companyid 改成了 id 并使用默认路由路由去掉了查询字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2012-01-16
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多