9

私は MVC3 Web アプリケーションを構築しており、knockoutjs を使用しています。アプリケーションには 2 つのビューがあります。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" }));
 }

上記は、ボタンがクリックされたときに以下の関数によって呼び出されます

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

また、クエリ文字列の代わりに以下の URL を取得するにはどうすればよいですか

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

1 に答える 1

22

AJAX を使用してRedirectToManageAccountアクション メソッドを呼び出すため、その応答を自分で処理する必要があり、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 });
    }
    
  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 () {
               }
      });
    }
    

2番目の質問について:

また、クエリ文字列の代わりに以下の URL を取得するにはどうすればよいですか http://localhost:53897/ManageAccount/Index2/7e96b930-a786-44dd-8576-052ce608e38f

RegisterRoutes()関数でこの URL の適切なルート エントリを定義するだけです。

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);
}
于 2012-08-17T13:02:15.103 に答える