woodsprite woodsprite - 3 months ago 9
ASP.NET (C#) Question

ASP.NET Index method not being called after return view

So I'm pretty new to ASP.Net and I'm having some issues. The problem is when I want to call upon a new controller and show the corresponding view, the index method in that controller does not get activated.

I have this code in my LoginController (I know this is insecure):

public ActionResult LoginTest(string inputEmail, string inputPassword, string submit)
{
if(submit != null)
{
Session["email"] = inputEmail;
return View("~/Views/Home/index.cshtml");
}
return null;
}


And this is the code in the controller/view I'm calling (HomeController):

public ActionResult Index()
{
if(Session["email"] != null)
{
ViewBag.HelloWorld = Session["email"].ToString();
} else
{
ViewBag.HelloWorld = "Does not work.";
}
return View();
}


Am I not supposed to do this through return View()? Is there a method that calls the controller which calls the corresponding view?

Any help is appreciated.

Answer

Use RedirectToAction in your LoginController instead of return View("~/Views/Home/index.cshtml");:

return RedirectToAction("Index", "Index");
Comments