简体   繁体   English

像cookie这样的全局变量,在回发后仍保留值,但应为不同的实例(选项卡/窗口)保存不同的值

[英]A global variable like cookie which retains value after postback, but it should save different value for different instances (tab/window)

This is an MVC3 web application. 这是一个MVC3 Web应用程序。 In the _Layout.cshtml shared view, there is a left-nav tree for loading pages on the right of the left-nav. _Layout.cshtml共享视图中,有一个左侧导航树,用于在左侧导航的右侧加载页面。 On every tree list item click, it will refresh the page to load the appropriate page with the same Layout view. 在每个树列表项上单击时,它将刷新页面以使用相同的布局视图加载适当的页面。

In this _Layout.cshtml, there is a Select Category link and an empty div ( targetDiv ) below the tree. 在此_Layout.cshtml中,树下面有一个“ Select Category链接和一个空的div( targetDiv )。 Select Category link will open a jQuery UI modal dialog with a webgrid and shows all the 'categories'. 选择类别链接将打开一个带有Webgrid的jQuery UI模式对话框,并显示所有“类别”。 On clicking on any 'category' link in the table, the dialog will close and targetDiv will be reloaded by AJAX, so that a webgrid inside targetDiv populates all the 'books' in this 'category'. 单击表中的任何“类别”链接后,该对话框将关闭,并且AJAX将重新加载targetDiv,以便targetDiv中的Webgrid会填充此“类别”中的所有“书籍”。

And if an user clicks on any left-nav item, the whole page reloads. 并且,如果用户单击任何左侧导航项,则会重新加载整个页面。 So I need to retain the 'books' list of the selected 'categories'. 因此,我需要保留所选“类别”的“书籍”列表。 For this, I save a cookie (CategoryId) when a 'category' is selected in modal dialog. 为此,当在模式对话框中选择“类别”时,我保存一个cookie(CategoryId)。 On page reload (document ready) I'm using this cookie in _Layout page which again populates all the 'books' in this 'category'. 重新加载页面(准备好文档)后,我在_Layout页面中使用了此Cookie,该页面再次填充了此“类别”中的所有“书籍”。

This is working fine, but the new requirement is to allow working on multiple 'Categories'. 这很好,但是新的要求是允许处理多个“类别”。 If I select 'Category 1' in one tab of a browser and get all books by this category, in the next tab (another instance of same app), I should be able to work on a different 'Category' and so different collection of books. 如果我在浏览器的一个标签中选择“类别1”,并按该类别获取所有图书,则在下一个标签(同一应用的另一个实例)中,我应该能够使用不同的“类别”,从而可以处理不同的图书。 In this case, I cannot use cookie because cookie is shared across all the tabs. 在这种情况下,我无法使用cookie,因为cookie在所有选项卡之间共享。

How will I set different 'Categories' on different instances (browser tabs/window)? 如何在不同的实例(浏览器标签/窗口)上设置不同的“类别”?

If I am understanding your question correctly, the easiest way would be to use AJAX postbacks to populate your targetDiv field rather than reloading the page each time. 如果我正确理解了您的问题,最简单的方法是使用AJAX回发来填充您的targetDiv字段,而不是每次都重新加载页面。 Then you would only be loading the relevant part of the page, so that you could have as many tabs open at once as you wanted without them interfering with each other. 然后,您将只加载页面的相关部分,这样就可以一次打开任意数量的选项卡,而不会彼此干扰。

MVC3 offers a lot of support for AJAX requests - just put the stuff that draws the grid in a partial view and it will be easy to change it when the user selects different options. MVC3为AJAX请求提供了很多支持-只需将绘制网格的内容放在局部视图中,当用户选择不同的选项时,很容易进行更改。

To help maintain state across AJAX requests you can just store it in a form field and pass the value in when you make your request - there are a few different ways to do this, either using an AjaxForm and submitting it with the request or adding the parameters to the request with JQuery depending on how you choose to implement your form. 为了帮助维护AJAX请求的状态,您可以将其存储在表单字段中,并在发出请求时将值传递给它-有几种不同的方法可以执行此操作,既可以使用AjaxForm并与请求一起提交,也可以添加使用JQuery的请求参数,具体取决于您选择如何实现表单。 You could store the current CategoryId as a hidden field and just add it into the request as it is created. 您可以将当前的CategoryId存储为隐藏字段,并在创建请求时将其添加到请求中。 If you have more than one category open on a single page, either a list of hidden fields with the same name to behave like a checkbox group or encoding a list in your hidden field should do the trick. 如果您在一个页面上打开了多个类别,则可以使用具有相同名称(类似于复选框组)的隐藏字段列表或对隐藏字段进行编码来解决问题。

I've solved this problem. 我已经解决了这个问题。 I've used Cookie-less Session Variables in JavaScript . 在JavaScript中使用了无Cookie会话变量 It basically stores your data in the window.name property which does not get cleared when you reload the page. 它基本上将您的数据存储在window.name属性中,该属性在您重新加载页面时不会清除。 And every tab has a different window object (this is what I want) and is supported in all major browsers. 每个选项卡都有一个不同的window对象(这是我想要的),并且在所有主要浏览器中都受支持。

This is just like session cookies, only difference is this data won't survive between windows/tabs which normal session cookies does. 就像会话cookie一样,唯一的区别是,这些数据将无法在窗口/选项卡之间正常生存。

Also see Session variables without cookies . 另请参阅没有cookie的会话变量

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

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