如何在没有帖子的情况下在 ASP.Net MVC 3 中保留值?

发布于 2025-01-07 06:50:05 字数 245 浏览 2 评论 0原文

我需要执行如下操作:

  1. 在页面上,当复选框状态更改时,对服务器执行 jQuery AJAX 调用以设置复选框的状态。
  2. 刷新将使用该值的 jqGrid(另一个 AJAX 调用)。

没有帖子。

在 ASP.Net 中,我会在视图状态中设置该值并从那里使用它。但是,viewstate 在 MVC 中无效。

我可以在哪里存储这个值,以便在该会话的所有后续调用中都可以使用该值?

I need to do something like the following:

  1. On the page, when a checkbox state is changed, do an jQuery AJAX call to the server to set the state of the checkbox.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2025-01-14 06:50:05

盒子还没勾选吗?只需重用来自客户端的值即可。如果您在通过 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.

落日海湾 2025-01-14 06:50:05

您可以使用 cookie 来存储此信息,因为它不是任何敏感信息。

查看 jQuery cookie 插件此处

示例:

//A cookie by the name 'checkboxstate' now exists with the value 'true'
$.cookies.set('checkboxstate', 'true'); 

//Variable 'checkBoxState' now holds the value 'true'
var checkBoxState = $.cookies.get( 'checkboxstate' ); 

//The cookie named 'checkboxstate' has been deleted.
$.cookies.del('checkboxstate'); 

有多种方法可以在客户端上保存数据或服务器端,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:

//A cookie by the name 'checkboxstate' now exists with the value 'true'
$.cookies.set('checkboxstate', 'true'); 

//Variable 'checkBoxState' now holds the value 'true'
var checkBoxState = $.cookies.get( 'checkboxstate' ); 

//The cookie named 'checkboxstate' has been deleted.
$.cookies.del('checkboxstate'); 

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.

鸩远一方 2025-01-14 06:50:05

您可以使用会话吗?

Session["value_name"] = Something.Text;

(有关会话的更多信息,请访问:http://msdn. microsoft.com/en-us/library/87069683(v=vs.85).aspx)

Are you able to use Sessions?

Session["value_name"] = Something.Text;

(More about Sessions here: http://msdn.microsoft.com/en-us/library/87069683(v=vs.85).aspx)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文