2012-08-24 78 views
10

RedirectToActionメソッドから複数のパラメータを渡したいRedirecttoactionのリストを渡す方法

どのようにすればいいですか?

マイアクションメソッド

[HttpPost, ActionName("SelectQuestion")] 
    public ActionResult SelectQuestion(string email,List<QuestionClass.Tabelfields> model) 
    { 

     List<QuestionClass.Tabelfields> fadd = new List<QuestionClass.Tabelfields>(); 
     for (int i = 0; i < model.Count; i++) 
     { 
      if (model[i].SelectedCheckbox == true) 
      { 
       List<QuestionClass.Tabelfields> f = new List<QuestionClass.Tabelfields>(); 
       fadd.Add(model[i]); 
      } 
     } 

     return RedirectToAction("Question", new { email = email, model = fadd.ToList() }); 
    } 

マイ別アクションメソッド

[HttpGet] 
    public ActionResult Question(string email,List<QuestionClass.Tabelfields> model) 
    { 
    } 

私はモデルの値を取得しておりません。

答えて

18

リダイレクト時にURLに複雑なオブジェクトを渡すことはできません。

一つの可能​​性はTempDataをを使用することです:

TempData["list"] = fadd.ToList(); 
return RedirectToAction("Question", new { email = email}); 

してから質問アクション内部:メソッドを作るために、ブラウザの原因となるブラウザにHTTP 302応答を返します

var model = TempData["list"] as List<QuestionClass.Tablefields>; 
+0

実際には私はメールのIDとモデルをURLに入れておく必要があります。 どうすれば入手できますか? –

1

RedirectToActionGET指定されたアクションの要求。

TempData/Sessionのような一時的な記憶領域にデータを保存するか、 TempDataは、バッキングストレージとしてSessionを使用します。

ステートレスをそのままにしたい場合は、クエリ文字列にIDを渡し、GETアクション内のアイテムのリストを取得する必要があります。本当にステートレス。

return RedirectToAction("Question", new { email = email,id=model.ID }); 

とあなたのGETメソッドで

public ActionResult Question(string email,int id) 
{ 

    List<QuestionClass.Tabelfields> fadd=repositary.GetTabelFieldsFromID(id); 
    //Do whatever with this 
    return View(); 
} 

はこれはもうおそらく有効ではありませんが、私はそれをやった方法を残しておきますIdを

+0

このオプションはありますか? RedirectToAction( "Question"、new {email = email、model = fadd.ToList()}); 私はあなたの答えから欲しいものを得られませんでした。 私は電子メールとリストを渡したいと思います。 ありがとうございました –

+1

@Ajay:私が言ったように、それはリダイレクト(新しいHTTPリクエストです。あなたはそのような複雑なオブジェクトをそのまま渡すことはできません)。あなたは上記のアプローチのいずれかを試みる必要があります(temp storage/Requery)。 HTTPはステートレスです。 – Shyju

4

からTabelFieldsの一覧をrepositary.GetTabelFieldsFromID戻りを想定すると、他の人を助けるためにここに。

私が代わりにRedirectToActionの簡単なリダイレクトを使用して、これを解く:

List<int> myList = myListofItems; 
var list = HttpUtility.ParseQueryString(""); 
myList.ForEach(x => list.Add("parameterList", x.ToString())); 
return Redirect("/MyPath?" + list); 

次に、あなたの他の方法に:

public ActionResult Action(List<int> parameterList){} 
0

私はこの問題を解決方法はにリストをシリアル化することでしたNewtonsoft.JsonのnugetパッケージのJsonConvertメソッドを使用してJSONオブジェクトを作成します。次に、シリアライズされたリストをパラメータとして渡してから、再度シリアライズして元のリストを再作成することができます。

だからあなたSelectQuestion方法であなたは、このコードを使用します。

return RedirectToAction("Question", 
    new { 
     email = email, 
     serializedModel = JsonConvert.SerializeObject(fadd.ToList()) 
    }); 

をそして、あなたの質問方法では、オブジェクトをデシリアライズするために、このコードを使用します。

[HttpGet] 
public ActionResult Question(string email, string serializedModel) 
{ 
    // Deserialize your model back to a list again here. 
    List<QuestionClass.Tabelfields> model = JsonConvert.DeserializeObject<List<QuestionClass.Tabelfields>>(serializedModel); 
} 
関連する問題