i have a super basic logic page in razorpages I am using sessions to save current user login credentials into a user model, i wanna display it after, but even though the currentuser isnt null, the currentuser.Name is empty for some reason
here is the code: Index.cshtml.cs
public User currentUser { get; set; }
[BindProperty]
public string LoginUserName { get; set; }
[BindProperty]
public string LoginPassword { get; set; }
public void OnPostLogin()
{
currentUser = new(LoginUserName.ToString(),LoginPassword.ToString());
HttpContext.Session.SetObjectsession("currentUser", currentUser);
}
Index.cshtml
Here it should display @reuslt.Name, but it displays just the "works" label that is checking if it goes into the right if
<form method="post" class="LoginContainer">
<label style="color:grey; font-size:17px; margin-left:20px;">Uživatelské jméno</label>
<input name="LoginUserName" class="LoginUsername" type="text"/>
<label style="color:grey; font-size:17px; margin-left:20px;">Heslo</label>
<input name="LoginPassword" class="LoginPassword" type="text" />
<input type="submit" class="Prihlasit" value="Přihlásit" asp-page-handler="Login"/>
@{
var reuslt = HttpContext.Session.GetObjectsession<User>("currentUser");
}
@if (reuslt != null)
{
<h1>@reuslt.Name</h1>
<h1>works</h1>
}
else
{
<h1>current user is null</h1>
}
TestSession.cs
public static class TestSession
{
//set session
public static void SetObjectsession(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
//get session
public static T GetObjectsession<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
UPDATE
i found the mistake, it was a very easy fix. In my model class i haven't saved the constructor sent variables into the User model variables
public string Name { get; set; }
public string Password { get; set; }
public User(string name, string password)
{
Name = name;
Password = password;
}
Sorry for unnecesary question