简体   繁体   English

将 object 存储在 session 变量中

[英]Store object in session variables

I have a dropdown menu that when you select an option value submit the form, and to avoid repetitive database calls I am storing my non-sensitive object in a session.我有一个下拉菜单,当您使用 select 选项值提交表单时,为了避免重复的数据库调用,我将非敏感 object 存储在 session 中。

private List<Employee> stafflist
{
    get { return Session["stafflist"] as List<Employee>; }
    set { Session["stafflist"] = new Employee(); }
}
private void RemoveStaff()
{
    Session.Remove("stafflist");
}

however in my然而在我的

[HttpPost]
public ActionResult index (...)
{
    //why can't I get the list of staff like this?
    ViewBag.staff=stafflist.Where(..).toList();

    //is the below still needed? i thought i 
    //have a session variable declare above, 
    //and to avoid 30x repetitive db calls? 
    //also note when i include the below the code runs fine, 
    //however, if i take it out it doesn't. i would like to avoid repetitive db calls
    stafflist=db.Employee.toList(); 
}

First of all, you should not prevent to query the database. 首先,您不应该阻止查询数据库。 Proper caching is hard to get right, and a database is perfectly capable of performing queries and caching data. 正确的缓存很难正确实现,并且数据库完全能够执行查询和缓存数据。

If you're absolutely sure you want to circumvent the database, and query clientside (ie in the controller) then you need to pull the entire staff list from the database at least once per visitor. 如果您绝对确定要绕过数据库并查询客户端(即在控制器中),则需要从数据库中至少每个访问者一次提取整个人员列表。

You could do that in the first GET call to this controller, assuming the user will always visit that: 假设用户将始终访问该控制器,则可以在对该控制器的第一个GET调用中执行此操作:

[HttpGet]
public ActionResult Index (...)
{
    var cachedStaff = db.Employee.toList(); 
    Session["stafflist"] = cachedStaff;
}

Then in the POST, where you actually want to do the database query (again, consider letting the database do what it's good at), you can query the list from the session: 然后在POST中,您实际上要执行数据库查询(同样,考虑让数据库执行其擅长的事情),您可以从会话中查询列表:

[HttpPost]
public ActionResult Index (...)
{
    var cachedStaff = Session["stafflist"] as List<Employee>();

    // TODO: check cachedStaff for null, for when someone posts after 
    // their session expires or didn't visit the Index page first.

    var selectedStaff = cachedStaff.Where(..).ToList();

    // the rest of your code
}

Then the property you introduced can be used as syntactic sugar to clean up the code a bit: 然后,您引入的属性可以用作语法糖来稍微清理一下代码:

private List<Employee> CachedStaff
{
    get { return Session["stafflist"] as List<Employee>; }
    set { Session["stafflist"] = value; }
}

[HttpGet]
public ActionResult Index (...)
{
    CachedStaff = db.Employee.toList(); 
}

[HttpPost]
public ActionResult Index (...)
{
    // TODO: this will throw an ArgumentNullException when
    //  the staff list is not cached, see above.
    var selectedStaff = CachedStaff.Where(..).ToList();

    // the rest of your code
}

A session is unique for the current user and the current session. 会话对于当前用户和当前会话是唯一的。 That means that when the user closes the browser, the session information is lost. 这意味着,当用户关闭浏览器时,会话信息将会丢失。 The session is also lost if the session cookie is removed. 如果删除会话cookie,则该会话也会丢失。 Read about state management . 阅读有关状态管理的信息

If you want to have a global staff list that is available for all users you need to use something else. 如果您想要一个可供所有用户使用的全局人员列表,则需要使用其他功能。 Caching is the most common case then. 缓存是最常见的情况。

you probably have it already figured it out, just in case I leave here what it worked for me.您可能已经弄清楚了,以防万一我离开这里对我有用。

First you create a new session variable based on an object created (in this case the object usr will be empty):首先,根据创建的 object 创建一个新的 session 变量(在这种情况下,object usr 将为空):

User usr = new User();
Session["CurrentUSR"]=usr;

where you want to use the new object, you will have to cast the session variable and point it to a new object created in that particular page:如果要使用新的 object,则必须转换 session 变量并将其指向在该特定页面中创建的新 object:

User usr= new User(); //at this point the usr object is empty, now you are going to replace this new empty object with the session variable created before
usr=Session["CurrentUSR"] as User();

In case you have a list, the best course of action would be to create a List<> of that particular object.如果您有一个列表,最好的做法是创建该特定 object 的List<>

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

相关问题 会话对象不存储值 - Session object doesn't store value 从包装类访问会话对象变量 - Access session object variables from a wrapper class 将下拉列表中的选定值存储在会话变量中 - Store Selected Values From Drop Down List in Session Variables 最轻量级的集合/泛型/对象,用于在会话中存储数据 - Most lightweight collection/generic/object to store data in session 将对象存储在asmx中以进行会话并在母版页中获取此值-获取null - Store object in asmx to session and get this value in master page - get null 如何在ASP.Net会话中仅存储对象的一部分? - How to store only a part of an object in ASP.Net session? 将 DataTable object 存储在 asp.net session 中的坏主意 - bad idea to store DataTable object in asp.net session 引用存储在Session变量中的对象的变量是否由GC收集? - Do variables that reference an object that is stored in a Session variable get collect by GC? 我可以创建一个“全局”对象来存储多个对象的变量吗? - Can I make a “global” object to store variables for multiple objects? 替换c#中的Session变量,我们可以使用类及其对象来存储值而不是会话变量吗? - Alternate for Session variable in c #, Can we use class and its object to store a value instead of session variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM