在knockoutjs中通过ajax jQuery调用MVC RedirectToAction无法正常工作

shr*_*aal 8 ajax asp.net-mvc-3 knockout.js

我正在构建一个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" }));
 }
Run Code Online (Sandbox Code Playgroud)

单击按钮时,上面的功能会调用上面的功能

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

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);
    }
Run Code Online (Sandbox Code Playgroud)

生成的URL是

http://localhost:53897/ManageAccount/Index2?companyId=7e96b930-a786-44dd-8576-
052ce608e38f
Run Code Online (Sandbox Code Playgroud)

Firebug中的控制台窗口显示

GET http://localhost:53897/ManageAccount/Index2?companyId=7e96b930-a786-44dd-8576-
052ce608e38f 200 OK and the spinner keeps spinng
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f
Run Code Online (Sandbox Code Playgroud)

two*_*wer 20

由于您使用AJAX来调用RedirectToManageAccountaction方法,因此您自己负责处理其响应,并且当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 });
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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 () {
               }
      });
    }
    
    Run Code Online (Sandbox Code Playgroud)

至于你的第二个问题:

另外,如何获取下面的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" }                            
);
Run Code Online (Sandbox Code Playgroud)

编辑:由于您的AJAX调用仅用于调用导致重定向的操作方法,您可以通过以下方式简化它,前提是您(在客户端)知道companyId已经:

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

我使用这种扩展方法的地方

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