如何在没有帖子的情况下在 ASP.Net MVC 3 中保留值?
我需要执行如下操作:
- 在页面上,当复选框状态更改时,对服务器执行 jQuery AJAX 调用以设置复选框的状态。
- 刷新将使用该值的 jqGrid(另一个 AJAX 调用)。
没有帖子。
在 ASP.Net 中,我会在视图状态中设置该值并从那里使用它。但是,viewstate 在 MVC 中无效。
我可以在哪里存储这个值,以便在该会话的所有后续调用中都可以使用该值?
I need to do something like the following:
- On the page, when a checkbox state is changed, do an jQuery AJAX call to the server to set the state of the checkbox.
- Refresh a jqGrid which will use this value (another AJAX call).
There is no post.
In ASP.Net, I would set the value in the viewstate and use it from there. However, viewstate is not valid in MVC.
Where can I store this value so it will be available on all subsequent calls to this page for this session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
盒子还没勾选吗?只需重用来自客户端的值即可。如果您在通过 AJAX 获取结果时替换它,则应该在返回 (AJAX) 响应之前在模型中设置该值,以便它在客户端上具有正确的值。如果该框代表某种首选项,选择一次即可适用于所有页面,然后将其存储在会话或某种持久性机制(cookie、SQL Server DB、RavenDB)中,并根据需要从那里检索它。
Isn't the box still checked? Just reuse the value from the client. If you're replacing it when you get the result via AJAX, you should be setting the value in the model before the (AJAX) response is returned so it has the proper value on the client. If the box represents some sort of preference, chosen once then applicable across all pages, then store it in the session or in some persistence mechanism (cookie, SQL server DB, RavenDB), retrieving it from there as needed.
您可以使用 cookie 来存储此信息,因为它不是任何敏感信息。
查看 jQuery cookie 插件此处
示例:
有多种方法可以在客户端上保存数据或服务器端,
cookie
就是其中之一。您可以查看此链接 http://msdn.microsoft.com/en -us/magazine/cc300437.aspx,它肯定会帮助您根据您的要求决定选择哪一本。You can use cookie to store this information since it is not any sensitive information.
Take a look at jQuery cookie plugin here
Examples:
There are various ways to persist data on the client or server side out of which
cookie
is one of them. You can take a look at this link http://msdn.microsoft.com/en-us/magazine/cc300437.aspx, it will definitely help you decide which one to go for based on your requirement.您可以使用会话吗?
(有关会话的更多信息,请访问:http://msdn. microsoft.com/en-us/library/87069683(v=vs.85).aspx)
Are you able to use Sessions?
(More about Sessions here: http://msdn.microsoft.com/en-us/library/87069683(v=vs.85).aspx)