2012-08-17 60 views
6

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

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); 
    } 

所生成的URL是下面的函数调用

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 

而且,我怎么得到下面的网址,而不是一个与查询字符串

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

回答

17

由于您使用AJAX调用RedirectToManageAccount操作方法,您有责任自行处理其响应,并且由于您的处理函数为空,因此您实际上将忽略任何作为响应而到达的处理函数。

如果你想从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() { 
          } 
        }); 
    } 
    

至于你的第二个问题:

而且,我怎么得到下面的网址,而不是一个与查询字符串 http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f

你只需要定义该网址在适当的路由条目您RegisterRoutes()功能:

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); 
} 
+0

有另一种方式来重定向到ManageAccount /索引2也是我加入的路线,你建议,但我还是得到了?companyId = XXXXX在URL – shresthaal 2012-08-17 13:33:08

+0

这取决于具体的情况,真的。我通过一种可能的重定向过程的简化来更新我的答案。至于路线:你确定你没有添加它__after__一些更一般的规则? – twoflower 2012-08-17 13:44:11

+0

我将companyid更改为id,并使用默认路由路径删除查询字符串 – shresthaal 2012-08-20 14:52:26