コントローラから別のコントローラにデータを渡す。私はこれは私がやっているものですが、私はそれを行うための正しい方法であると思ういけない、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を使用するかもしれませんが、私はそれを一緒に置く方法がわかりません...ありがとう!
こんにちはステファン、より詳細な情報を確認してください、ありがとう!あなたの助けを感謝する、あなたは私があなたが意味するもので私を例証することができますか、またはあなたはGET CreateのModel CompanyInformationにそれを保持することができます、このModelViewですか?あなたに私に例を教えてください... – Ben
あなたの 'CompanyInformation'がUserIdプロパティを持っていれば、あなたの作成アクション' return View(new CompanyInformation {UserId = id});でこれを行うことができます例えば@Html.HiddenFor(x => x)のような非表示フィールドのビュー。UserId) ' –
ご質問申し訳ありませんが、最初の例は で、URLを介してデータを渡す必要があり、URLにデータを表示せずにデータを渡したい場合は可能ですか?どうやってやるの?再度、感謝します! – Ben