简体   繁体   English

ASP.NET MVC会话变量不起作用-保留为null

[英]ASP.NET MVC Session Variables not working - remaining as null

I am trying to store an object of the type 'Person' in a session variable. 我正在尝试在会话变量中存储“ Person”类型的对象。 The Session variable is stored like so: Session变量的存储方式如下:

    public void SelectPerson(int ID)
    {
        Person person = db.People.Where(p => p.ID == ID).FirstOrDefault();

        Session["PersonID"] = ID;
        Session["Person"] = person;
    }

I then try and use this session variable at a later date, but it is always coming back as null 然后,我尝试在以后使用此会话变量,但它总是以null返回

Person person = (Person) Session["Person"];

I am not sure why this isn't working. 我不确定为什么这行不通。 Does anyone have any ideas? 有人有什么想法吗? Thanks for your help. 谢谢你的帮助。

Please check what is the value that you get from this line: Person person = db.People.Where(p => p.ID == ID).FirstOrDefault(); 请检查从此行获得的值是什么: Person person = db.People.Where(p => p.ID == ID).FirstOrDefault();

Try to write: 尝试写:

Response.Write(person); Response.Write(person);

After you tried what I've wrote here, tell me the value that it writes. 在尝试了我在这里写的内容之后,请告诉我它所写的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM