我正在構建一個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
有另一種方式來重定向到ManageAccount /索引2也是我加入的路線,你建議,但我還是得到了?companyId = XXXXX在URL – shresthaal 2012-08-17 13:33:08
這取決於具體的情況,真的。我通過一種可能的重定向過程的簡化來更新我的答案。至於路線:你確定你沒有添加它__after__一些更一般的規則? – twoflower 2012-08-17 13:44:11
我將companyid更改爲id,並使用默認路由路徑刪除查詢字符串 – shresthaal 2012-08-20 14:52:26