ASP.NET MVC - 有没有一种方法来模拟一个ViewState的? [英] ASP.NET MVC - Is there a way to simulate a ViewState?

查看:143
本文介绍了ASP.NET MVC - 有没有一种方法来模拟一个ViewState的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的情况......在某视图,用户必须选择初始小时,最后一个小时和平日。但是,我不能把这信息保存到数据库,因为我要救我的整个页面,我需要主表的主键,但是这不是问题的关键。

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.

所以,虽然我没有保存这些数据到数据库,我保存到会话。有人告诉我,保存到一个cookie,但现在看来,饼干有大小限制。所以,我保存到会话。

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,我还被告知,我可以(小时工作日)保存这些信息到用户页面,模拟ASP.NET的ViewState ...

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

有谁知道如何做到这一点?有谁知道如何保存这些临时数据withou使用Cookie或会话??

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

谢谢!

推荐答案

隐藏输入字段不会帮助?

Hidden input fields won't help?

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

更新(序列化对象为Base64):

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());

当你想将它取回来:

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

请确保当您使用它作为客户端可能会改变它,你验证存储在字段中的数据。 的ViewState 自动处理这一点。

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.

边注:的ASP.NET使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.losformatter.aspx\"><$c$c>LosFormatter而不是的BinaryFormatter 的序列化的ViewState ,因为它的效率更高或ASCII基于序列化。你可能要考虑这一点。

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.

这篇关于ASP.NET MVC - 有没有一种方法来模拟一个ViewState的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆