gpt4 book ai didi

c# - 返回 View 后未调用 ASP.NET 索引方法

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:24 25 4
gpt4 key购买 nike

所以我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com