将数据从控制器传递到另一个控制器。这是我在做什么,但我不认为这是做这件事的正确方法,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...
}
我希望这是清楚什么,我试图做的。也许使用ViewModels,但我不知道如何把它放在一起......谢谢!
斯特凡喜找到更多的细节,谢谢!感谢你的帮助,你可以通过你的意思来示范我“或者你可以将它保存到GET Create的ModelInformation模型中,这是ModelView吗?你能否给我提供一个例子...... – Ben 2011-12-16 07:28:48
我的意思是如果你的`CompanyInformation`具有UserId属性,那么你可以在你的创建操作`返回视图(新的CompanyInformation {UserId = id});`中做这样的事情并把它保存到例如`@ Html.HiddenFor(x => x)的隐藏字段中的视图。UserId)` – 2011-12-16 07:42:27