当前会话用户未在Razor页面中显示。

huangapple go评论75阅读模式
英文:

current session user not displaying in razorpages

问题

以下是您提供的内容的翻译:

我有一个非常基本的Razor Pages逻辑页面。
我正在使用会话来保存当前用户的登录凭据到一个用户模型中,之后我想显示它,但即使当前用户不是null,当前用户的Name属性也为空,原因不明。

以下是代码部分:
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 <br>
这里应该显示@result.Name,但它只显示检查是否进入正确if语句的"label"。

&lt;form method="post" class="LoginContainer"&gt;
&lt;label style="color:grey; font-size:17px; margin-left:20px;"&gt;Username&lt;/label&gt;
&lt;input name="LoginUserName" class="LoginUsername" type="text"/&gt;
&lt;label style="color:grey; font-size:17px; margin-left:20px;"&gt;Password&lt;/label&gt;
&lt;input name="LoginPassword" class="LoginPassword" type="text" /&gt;
&lt;input type="submit" class="Login" value="Login" asp-page-handler="Login"/&gt;
@{
    var result = HttpContext.Session.GetObjectsession<User>("currentUser");
}

@if (result != null)
{
    &lt;h1&gt;@result.Name&lt;/h1&gt;
    &lt;h1&gt;It works&lt;/h1&gt;
}
else
{
    &lt;h1&gt;Current user is null&lt;/h1&gt;
}

</form>

TestSession.cs

public static class TestSession
{
    //设置会话
    public static void SetObjectsession(this ISession session, string key, object value)
    {
        session.SetString(key, JsonConvert.SerializeObject(value));
    }

    //获取会话
    public static T GetObjectsession<T>(this ISession session, string key)
    {
        var value = session.GetString(key);
        return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
    }
}
英文:

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(&quot;currentUser&quot;, currentUser);

    }

Index.cshtml <br>
Here it should display @reuslt.Name, but it displays just the "works" label that is checking if it goes into the right if

&lt;form method=&quot;post&quot; class=&quot;LoginContainer&quot;&gt;
&lt;label style=&quot;color:grey; font-size:17px; margin-left:20px;&quot;&gt;Uživatelsk&#233; jm&#233;no&lt;/label&gt;
&lt;input name=&quot;LoginUserName&quot; class=&quot;LoginUsername&quot; type=&quot;text&quot;/&gt;
&lt;label style=&quot;color:grey; font-size:17px; margin-left:20px;&quot;&gt;Heslo&lt;/label&gt;
&lt;input name=&quot;LoginPassword&quot; class=&quot;LoginPassword&quot; type=&quot;text&quot; /&gt;
&lt;input type=&quot;submit&quot; class=&quot;Prihlasit&quot; value=&quot;Přihl&#225;sit&quot; asp-page-handler=&quot;Login&quot;/&gt;
@{
    var reuslt = HttpContext.Session.GetObjectsession&lt;User&gt;(&quot;currentUser&quot;);
}

@if (reuslt != null)
{
    &lt;h1&gt;@reuslt.Name&lt;/h1&gt;
    &lt;h1&gt;works&lt;/h1&gt;
}
else
{
    &lt;h1&gt;current user is null&lt;/h1&gt;
}

</form>

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&lt;T&gt;(this ISession session, string key)
    {
        var value = session.GetString(key);
        return value == null ? default(T) : JsonConvert.DeserializeObject&lt;T&gt;(value);
    }
}

答案1

得分: 0

UPDATE

我发现了错误,这是一个非常简单的修复。
在我的模型类中,我没有将构造函数传递的变量保存到User模型的变量中

public string Name { get; set; }
public string Password { get; set; }

public User(string name, string password)
{
    Name = name;
    Password = password;
}

抱歉造成不必要的问题。

英文:

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

huangapple
  • 本文由 发表于 2023年5月6日 18:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定