我希望我的索引控制器根据登录用户的角色做两件事:
这是我遇到麻烦的第二个动作。请考虑以下事项:
public ActionResult Index()
{
if (Session["userid"] != null)
{
if (String.Compare(Convert.ToString(Session["userrole"]), "Master", StringComparison.InvariantCultureIgnoreCase) == 0)
return View(db.Companies.ToList());
return View("Edit", new Id = Convert.ToInt32(Session["companyid"])) // this doesn't work
}
return RedirectToAction("Index", "Home");
}
如何将用户重定向到另一个视图并预加载所需的数据?
提前致谢!
答案 0 :(得分:1)
public ActionResult Index()
{
if (Session["userid"] != null)
{
if (String.Compare(Convert.ToString(Session["userrole"]), "Master", StringComparison.InvariantCultureIgnoreCase) == 0)
return View(db.Companies.ToList());
return RedirectToAction("Edit", new { id = Convert.ToInt32(Session["companyid"]});
}
return RedirectToAction("Index", "Home");
}
public ActionResult Edit(int id)
{
///Get your company by id here
}