2012-08-17 78 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