0

同じコントローラーの別のアクションにリダイレクトし、1 つのパラメーター値を渡したいです。私はこのコードを持っています:

public ActionResult Index()
{

}

public ActionResult SomeAction()
{
  if (isFailed)
  {
    // Redirect to Index Action with isFailed parameter so that some message gets displayed on Index page.
  }
 }

使用について読みましTempDataたが、私のセッションは(何らかの目的で)読み取り専用であるため、にデータを保存するTempDataと、実際には空であるSomeActionため役に立ちません。TempDataIndex

私が試したもう1つのことは、RedirectToAction("Index","Test", new { param = isFailed})in returnstatement of SomeAction. これは機能し、 を使用してparam実際にアクセスできますが、これに関する問題は、URL が必要な場所になることです。IndexRequest.QueryString['param']/AreaName?param=true/AreaName/Test/

これは私のルートマップです:

        context.MapRoute(
            "default",
            "AreaName/{controller}/{action}/{param}",
            new { controller="Test", action = "Index", param=UrlParameter.Optional },
        );

MyJS.js から「post」を使用してフォームを送信していSomeActionます。

これを行うための代替/回避策はありますか? ざっくり言うと、以下の3つが欲しいです。

  1. アクションにリダイレクトしIndexます。
  2. paramfromの値をSomeActionに渡しますIndex
  3. URL を保持します。http://localhost/AreaName/Test/
4

2 に答える 2

1

これを試して

public ActionResult Index(Datatype param)
{

}
public ActionResult SomeAction()
{
if (isFailed) 
{
     return RedirectToAction("Index" , "Home",new{param= value });
}    
return View();
}
于 2013-06-18T17:56:16.020 に答える
0
  • アクションにリダイレクトしIndexます。

使用するreturn RedirectToAction("Index","Home");

  • param の値を からSomeActionに渡しますIndex

TempDataオブジェクト を使用できます。

TempData プロパティの値は、セッション状態に格納されます。TempDataDictionary 値が設定された後に呼び出されるすべてのアクション メソッドは、オブジェクトから値を取得し、それらを処理または表示できます。TempData の値は、読み取られるか、セッションがタイムアウトするまで保持されます。

以下のようにできます。

public ActionResult Index()
{
    //you can access TempData here.
}

public ActionResult SomeAction()
{
  if (isFailed)
  {
      TempData["Failure"] = "Oops, Error";  //store to TempData
      return RedirectToAction("Index" , "Home");
  }
  return View();
 }

この MSDN の記事ですべてが説明されています。

  • URL を保持します。http://localhost/AreaName/Test/

ルートを追加することでこれを行うことができますRouteConfig.cs

context.MapRoute(
            "default",
            "AreaName/Test/",
            new { area = "AreaName" controller="Test", action = "Index"},
        );
于 2013-06-18T17:34:25.193 に答える