ASP.NET MVC - 有没有办法模拟 ViewState?

发布于 2024-07-15 17:49:15 字数 330 浏览 4 评论 0原文

我有以下情况...在某个视图中,用户必须选择初始小时、最终小时和工作日。 但是,我无法将此信息保存到数据库中,因为我需要保存整个页面并且需要主表的主键,但这不是重点。

因此,虽然我不将这些数据保存到数据库,但我将其保存到会话。 我被告知要保存到 cookie,但 cookie 似乎有大小限制。 所以,我将保存到会话中。

Buuuut,我还被告知我可以将这些信息(小时和工作日)保存到用户页面,模拟 ASP.NET ViewState...

有人知道如何做到这一点吗? 有谁知道如何在不使用cookie或Session的情况下临时保存这些数据?

谢谢!!

I have the following situation... In a certain View, the user must select the initial hour, the final hour and the weekday. But, I can't save this informations to DB 'cause I need to save my whole page and I need the primary key of the primary table, but that's not the point.

So, while I don't save these data to DB, I'm saving to a Session. I was told to save to a cookie, but it appears that cookies have a size limit. So, I'm saving to a Session.

Buuuut, I was also told that I could save these informations (hours and weekday) to the user page, simulating a ASP.NET ViewState...

Does anyone know how to do this?? Does anyone know how to save temporarily these data withou using cookie or Session??

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

葬﹪忆之殇 2024-07-22 17:49:15

隐藏的输入字段没有帮助吗?

<%= Html.Hidden(...) %>

更新(将对象序列化为 base64):

var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, myObject); // myObject should be serializable.
string result = Convert.ToBase64String(stream.ToArray());

当您想要将其取回时:

var formatter = new BinaryFormatter();
var stream = new MemoryStream(Convert.FromBase64String(hiddenFieldValue));
var myObject = (MyObjectType)formatter.Deserialize(stream);

请确保在使用该字段时验证存储在该字段中的数据,因为客户端可能会更改它。 ViewState 自动处理这个问题。

附注: ASP.NET 使用 LosFormatter 而不是 BinaryFormatter 来序列化 ViewState,因为它更有效或基于 ASCII 的序列化。 您可能也想考虑这一点。

Hidden input fields won't help?

<%= Html.Hidden(...) %>

Update (serializing an object to base64):

var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, myObject); // myObject should be serializable.
string result = Convert.ToBase64String(stream.ToArray());

When you want to fetch it back:

var formatter = new BinaryFormatter();
var stream = new MemoryStream(Convert.FromBase64String(hiddenFieldValue));
var myObject = (MyObjectType)formatter.Deserialize(stream);

Make sure you validate the data stored in the field when you use it as the client might change it. ViewState takes care of this automatically.

Side note: ASP.NET uses LosFormatter instead of BinaryFormatter to serialize ViewState as it's more efficient or ASCII based serialization. You might want to consider that too.

厌倦 2024-07-22 17:49:15

TempData["MyData"],请注意,这只会持续一次往返。

TempData["MyData"], mind you this will only last one round trip.

梦里的微风 2024-07-22 17:49:15

您可以在客户端上保存一个 JavaScript 数组...然后在用户最终保存时传输所有信息。

你必须多努力一点,但最终会有回报。

我大量使用 jQuery 来做类似的事情,它比看起来更容易。

You could save a javascript array on the client... and then transmit all the information when the user ultimately saves.

You have to work a little more, but in the end it pays off.

I heavily use jQuery to do stuff like that, it's easier than it seems.

橘和柠 2024-07-22 17:49:15

如果您只想保存该请求和下一个请求的数据,我建议使用 Tempdata,否则我建议使用 Mehrdad 的答案。

If you just want to save the data for that request and the next request I'd recommend using Tempdata, else I'd recommend using Mehrdad`s answer.

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