2011-12-16 21 views
0

コントローラから別のコントローラにデータを渡す。私はこれは私がやっているものですが、私はそれを行うための正しい方法であると思ういけない、plzは、それを動作させるために私は、コードを変更役立つ例/チュートリアルを共有...MVC3コントローラからコントローラへデータを渡す方法

例えば

会員のAPIを使用すると、ユーザーアカウントを作成し

public ActionResult Register() { return View(); } 

[HttpPost] 
public ActionResult Register(RegisterModel model) 
{ 
    //creates an account and redirect to CompanyController 
    //Also I want to store the userId and pass it to the next controller, I am using a session, ok? 
    Session["userObject"] = userIdGenerated() 
    return RedirectToAction("Create", "Company");   
} 

CompanyController:

public ActionResult Create() { return View(); } 

[HttpPost] 
public ActionResult Create(CompanyInformation companyinformation) 
{ 
    //creating company account and I need to store the userid to the company table retrieving from a session 
    companyinformation.UserID = Session["userObject"].ToString(); 
    db.CompanyInformation.Add(companyinformation); 
    db.SaveChanges(); 

    //retrieving companyId that was generated and need to pass to the next controller I tried to use "TempData["companyId"] = companyinformation.CompanyInformationID" But the data is no longer found on httpPost 

return RedirectToAction("Create", "Contact"); 

}

問い合わせコントローラ

public ActionResult Create() 
    { 
    //I tried using ViewBag to store the data from TempDate but the data is no longer found on httpPost 
     ViewBag.companyId = TempData["companyId"].ToString(); 
     return View(); 
    } 

[HttpPost] 
public ActionResult Create(CompanyContact companycontact) 
{ 
    companycontact.CompanyInformationID = ???? How do I get the companyId? 
    db.CompanyContacts.Add(companycontact); 
    db.SaveChanges(); 
    //Redirect to the next controller... 
} 

私は私がやろうとしています何明らかであると思います。たぶんViewModelを使用するかもしれませんが、私はそれを一緒に置く方法がわかりません...ありがとう!

答えて

1

それは

RedirectToActionあなたはrouteValuesを設定することを可能にする1 overloadを持って標準ナビゲーション流れだったとしてあなたは、コントローラのメソッドに直接ごUserIDパラメータを渡すことができます。

return RedirectToAction("Create", "Company", new { id = userIdGenerated() });  

そして、あなたのCompanyController

public ActionResult Create(int id) { return View(id); } 

であなたがURLでごidを持つことになりますので、あなたは同様にあなたの記事でそれをキャッチすることができるだろう:

[HttpPost] 
public ActionResult Create(int id, CompanyInformation companyinformation) 

かモデルにそのまま残すことができますCompanyInformation GET Create

+0

こんにちはステファン、より詳細な情報を確認してください、ありがとう!あなたの助けを感謝する、あなたは私があなたが意味するもので私を例証することができますか、またはあなたはGET CreateのModel CompanyInformationにそれを保持することができます、このModelViewですか?あなたに私に例を教えてください... – Ben

+0

あなたの 'CompanyInformation'がUserIdプロパティを持っていれば、あなたの作成アクション' return View(new CompanyInformation {UserId = id});でこれを行うことができます例えば@Html.HiddenFor(x => x)のような非表示フィールドのビュー。UserId) ' –

+0

ご質問申し訳ありませんが、最初の例は で、URLを介してデータを渡す必要があり、URLにデータを表示せずにデータを渡したい場合は可能ですか?どうやってやるの?再度、感謝します! – Ben

1

あなたはそれのためにTempDataを使用することができます。

下のリンク http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

+0

ここにシナリオがあります。ユーザーがController1に行くと、TempData(userId)が作成され、Controller2にリダイレクトされ、その値が使用された後、ユーザーはControlle1とController2に戻りますが、値は使用できなくなります。あなたはどうやってそれを扱いますか?コントローラ間でどのように価値を維持するのですか? thx – Ben

+0

Controller1の値を簡単に取得できるように、controller2のTempDataに再度useridを割り当てます。 –

関連する問題