所以我是 ASP.Net 的新手,遇到了一些问题。问题是当我想调用一个新的 Controller 并显示相应的 View 时,该 Controller 中的索引方法没有被激活。
我的 LoginController 中有这段代码(我知道这是不安全的):
public ActionResult LoginTest(string inputEmail, string inputPassword, string submit)
{
if(submit != null)
{
Session["email"] = inputEmail;
return View("~/Views/Home/index.cshtml");
}
return null;
}
这是我调用的 Controller / View 中的代码 (HomeController):
public ActionResult Index()
{
if(Session["email"] != null)
{
ViewBag.HelloWorld = Session["email"].ToString();
} else
{
ViewBag.HelloWorld = "Does not work.";
}
return View();
}
难道我不应该通过 return View() 来做到这一点吗?有没有调用 Controller 的方法调用相应的 View ?
感谢任何帮助。
最佳答案
使用RedirectToAction
在你的 LoginController
而不是 return View("~/Views/Home/index.cshtml");
:
return RedirectToAction("Index", "Index");
关于c# - 返回 View 后未调用 ASP.NET 索引方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793095/