ASP.NET MVC - 有没有办法模拟 ViewState?
我有以下情况...在某个视图中,用户必须选择初始小时、最终小时和工作日。 但是,我无法将此信息保存到数据库中,因为我需要保存整个页面并且需要主表的主键,但这不是重点。
因此,虽然我不将这些数据保存到数据库,但我将其保存到会话。 我被告知要保存到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
隐藏的输入字段没有帮助吗?
更新(将对象序列化为 base64):
当您想要将其取回时:
请确保在使用该字段时验证存储在该字段中的数据,因为客户端可能会更改它。
ViewState
自动处理这个问题。附注: ASP.NET 使用
LosFormatter
而不是BinaryFormatter
来序列化ViewState
,因为它更有效或基于 ASCII 的序列化。 您可能也想考虑这一点。Hidden input fields won't help?
Update (serializing an object to base64):
When you want to fetch it back:
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 ofBinaryFormatter
to serializeViewState
as it's more efficient or ASCII based serialization. You might want to consider that too.TempData["MyData"],请注意,这只会持续一次往返。
TempData["MyData"], mind you this will only last one round trip.
您可以在客户端上保存一个 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.
如果您只想保存该请求和下一个请求的数据,我建议使用 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.