简体   繁体   English

MVC2中隐藏字段的替代方法

[英]Alternatives to hidden fields in MVC2

I have a viewModel that contains a string that is populated before being passed to a partial view. 我有一个viewModel包含一个字符串,该字符串在传递给局部视图之前已填充。 I need to be able to get the data back when the form is posted. 发布表单后,我需要能够取回数据。 At the moment I have created a hidden field and bound the data to it. 目前,我已经创建了一个隐藏字段并将数据绑定到该字段。 Then when posting back I can get the data from the form collection. 然后,当发回时,我可以从表单集合中获取数据。

This isn't exactly what I would like. 这不是我想要的。 I would prefer for the data to be entirely hidden from view. 我希望数据完全不可见。 Using sessions aren't really an option for the framework I have to adhere to. 对于必须遵循的框架,使用会话并不是真正的选择。

Is there any alternative I can use? 我可以使用其他替代方法吗?

Thanks 谢谢

A bit of clarification is needed here: is the important thing that the data is not visible to the user, or that it is not accessible ? 这里需要做一点澄清:重要的是数据对用户不可见或不可访问吗?

If you want to make it invisible , you could store the data in a cookie instead of a hidden field. 如果要使其不可见 ,可以将数据存储在cookie中,而不是隐藏字段中。 When you read it on the server, you also mark the cookie as expired. 在服务器上读取它时,还将cookie标记为已过期。 It's a bit of a hack, but it will do what you ask for - store data on client side but not in the markup. 这有点麻烦,但是它将满足您的要求-将数据存储在客户端,而不是存储在标记中。

If you want to make it inaccessible , you could either obfuscate it (hash or encrypt it, for example) and still store it in a hidden field (or cookie, as above), but knowing that since you still give the user some part of the information, it is not entirely impossible to access the data. 如果您想使其无法访问 ,则可以对其进行混淆(例如,对其进行哈希处理或加密),然后仍将其存储在一个隐藏字段(或cookie,如上)中,但是要知道,因为您仍然为用户提供了信息,并不是完全不可能访问数据。

If the important thing is to store data where the user cannot, in any way, access it, but you're unable to do so on the server side, you're out of luck. 如果重要的是将数据存储在用户无法以任何方式访问的位置,但是您无法在服务器端进行访问,那么您很不走运。 Anything you send to the client, a smart enough user can read. 您发送给客户端的任何内容,都足够聪明的用户可以阅读。

Keep it stateless 保持无状态

If at all possible keep your requests completely stateless . 尽可能让您的请求完全无状态 I would avoid storing data in a session. 我会避免在会话中存储数据。 I'd rather encrypt/obfuscate data and put it in a hidden field/cookie than introduce state. 我宁愿加密/混淆数据并将其放在隐藏的字段/ cookie中,也不要引入状态。 It will make it harder on you later on. 以后您会很难受的。 Believe me. 相信我。

Can you explain a bit more why you need to preserve this state? 您能否进一步解释为什么需要保留此状态?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM