So I searched up how to store a object in a session, but they say I have to do it like this:
Session["object"] = wiz;
/// wiz is a object
I have to use:
HttpContext.Session.
To even access session, so that code is obviously not working as it's not recognized.`
Homecontroller:
public IActionResult Index(UserVM gebruiker)
{
UserVM user = new(UC.FindUserByEmail(gebruiker.Email));
return View(user);
}
LoginController:
public ActionResult Index()
{
UserVM vm = new();
vm.Retry = false;
return View(vm);
}
[HttpPost]
public IActionResult Index(UserVM user)
{
User u = UC.FindUserByEmailAndPassword(user.Email, user.Password);
if (u != null)
{
HttpContext.Session.SetInt32("UserId", u.UserId);
return RedirectToAction("Index", "Home", user);
}
else
{
UserVM vm = new();
vm.Retry = true;
return View(vm);
}
Usermodel:
public UserVM(string firstname, string lastname, string email, DateTime birthdate, double weight, int length)
{
FirstName = firstname;
LastName = lastname;
Email = email;
BirthDate = birthdate;
Weight = weight;
Length = length;
}
public UserVM(User user)
{
this.FirstName = user.FirstName;
this.LastName = user.LastName;
this.Email = user.Email;
this.BirthDate = user.BirthDate;
this.Weight = user.Weight;
this.Length = user.Length;
}
public UserVM()
{
}
My shared navbar which needs to send a UserVM to the homepage again. As why I was trying to put a model in the session:
@using Microsoft.AspNetCore.Http
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index" asp-route-gebruiker="@ViewBag.User">Home</a>