I have tried to accessing session from one view to another view using ASP.NET Core 6. The session displays ok in the Index
view, but did not show in ChangePassword
view. Please assist me the session to show in the ChangePassword
view.
public IActionResult Index()
{
ViewBag.Name = HttpContext.Session.GetString("username");
return View();
}
Index view:
<h4> Hello: @ViewBag.Name </h4>
public IActionResult ChangePassword()
{
ViewBag.Email = HttpContext.Session.GetString("username");
return View();
}
ChangePassword
view:
<input type="text" value="@ViewBag.Name" />
public IActionResult Login(Login model)
{
HttpContext.Session.SetString("username", “Dauda”);
return RedirectToAction("Index", "Student");
}
< input type="text" value="@ViewBag.Name" />
In your ChangePassword view, you use @ViewBag.Name
not @ViewBag.Email
, so change your ChangePassword action like:
public IActionResult ChangePassword()
{
ViewBag.Name = HttpContext.Session.GetString("username");
return View();
}
result: