我正在构建一个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