0

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>

Meadas
  • 89
  • 6
  • `HttpContext.Current.Session`? – VDWWD Apr 12 '22 at 15:56
  • @VDWWD `HttpContext.Current.` Does not even exist even tho I'm using `using Microsoft.AspNetCore.Http;` – Meadas Apr 12 '22 at 16:06
  • 2
    Have you seen [the documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-6.0#session-state)? – Crowcoder Apr 12 '22 at 17:22
  • @Meadas Have you tried importing the `using Microsoft.AspNetCore.Mvc` namespace? – Rahul Sharma Apr 12 '22 at 17:31
  • Where are you trying to access it? Are you in a controller or page? – Ben Matthews Apr 13 '22 at 01:41
  • @RahulSharma I am, but still not working. – Meadas Apr 13 '22 at 07:26
  • @BenMatthews In my controller – Meadas Apr 13 '22 at 07:27
  • @Crowcoder I did..... – Meadas Apr 13 '22 at 07:27
  • @Meadas Can you show us more code regarding your problem like your `Controller` class – Rahul Sharma Apr 13 '22 at 07:28
  • @Meadas What is the exact error that you are getting for this ? – Rahul Sharma Apr 13 '22 at 08:08
  • @RahulSharma `System.Data.SqlClient.SqlException: 'The parameterized query '(email nvarchar(4000))SELECT * FROM Users WHERE Email = email' expects the parameter 'email', which was not supplied.' ` So my home controller index needs a UserVM object, but when I click on the home button on the navbar it does not send a UserVm with it. Which is why there is no email supplied. – Meadas Apr 13 '22 at 08:14
  • @Meadas But this question is about some other issue regarding your `Session` variable. Please update the question with relevant problem or post a new question. Are you trying to access you `Session` variabe on your `View`? Are you able to set you session in your `Controller` method? – Rahul Sharma Apr 13 '22 at 08:19
  • @RahulSharma I am trying to put a object in a session, but I can't the code is not allowing me? That is the whole problem. – Meadas Apr 13 '22 at 08:24
  • @Meadas You can refer to this question on how to store and retrieve a complex model in your `Session`: https://stackoverflow.com/questions/55220812/how-to-store-and-retrieve-objects-in-session-state-in-asp-net-core-2-x/55221661#55221661 – Rahul Sharma Apr 13 '22 at 08:32

0 Answers0