在没有会话或查询字符串的情况下在 MVC2 中保持状态的方法?
有没有办法在 MVC2 中的回发之间保留信息而不使用会话变量或查询字符串?
Is there a way to keep information between post backs in MVC2 without utilizing Session variables or the query string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果 Session 变量的主要问题是实际问题(想要适用于单个请求的东西,而不需要担心清理等),而不是要求不使用 Session,那么请使用 TempData 字典。它仅针对单个请求将信息放入会话中,之后框架会自动为您删除它。
If your main issue with the Session variables is of a practical nature (want something that works for a single request, not needing to worry about cleaning it up etc) rather than a requirement to not use Session then use the TempData dictionary. It deals with putting information in the Session for a single request only and the framework will automatically remove it for you afterwards.
您的意思是像 .NET Web Forms 中的视图状态一样?从技术上讲,虽然不建议这样做,但最好利用模型并将模型数据发布到服务器并将模型推回到视图中。
这会很好地工作,但如果您需要像 WebForms ViewState 这样有状态的东西,我建议您在 WebForms 中完成您的项目或使用会话来保存您的模型。
编辑:构建将数据发布(或获取)回同一页面的表单。然后在你的控制器中,有一个这样的方法。
这会将用户刚刚提交的表单数据推回到您的视图中。那么在你看来就有一个像这样的 Html 助手了。
网络上有大量关于将 html 助手与模型结合使用的优秀资源。谷歌一下,你就会发现它们。
You mean like the view state from .NET Web Forms? Technically there is, although it isn't recommended - you're much better off utilizing models and posting the model data to the server and pushing the model back into the view.
This will work well but if you're needing something as stateful as the WebForms ViewState, I would recommend doing your project in WebForms or use the session to save your models.
Edit: Build your form that posts (or gets) data back to the same page. Then in your controller, have a method like this.
This will push the form data that the user just submitted back into your view. Then have an Html helper like this in your view.
There are a slew of excellent resources on the web about using html helpers with models. Google around and you'll come across them.
您可以使用隐藏表单字段在每次提交表单时将值 POST 回服务器。
You could use Hidden Form fields to POST the values back to the server with each form submission.
其他替代方案包括 cookie 或 Http 缓存 - 是什么阻止您使用会话?
Other alternatives include a cookie or Http Cache - what is stopping you using session?
作为一个高级概念,您不仅应该尽可能少地依赖 Session 来存储状态,而且还应该尽可能少地依赖 Web 应用程序中的状态性。这个想法是,网络本身在设计上是无状态的,并且在基于该范例设计软件时,软件应该被设计为包含无状态性质。
更具体地说,使用 ViewModel 为您提供视图传递给客户端所需的数据的强类型表示。保存有关可以从该视图发出的给定请求的状态信息的数据片段可能可以通过多种方式添加到视图中,但最直接的两个是:
查看 NerdDinner 教程,了解使用 ViewData 或强类型化的 ViewModel。一些谷歌搜索将一如既往地产生更多信息和教程。但请特别注意教程在视图中使用 ViewModel 属性的位置。这些可以在 HTML 渲染中的任何地方使用,无论是在 HTML 帮助器中还是在手动构建标签中。
关于网络无状态性的其他有趣阅读(以及这整个不那么新的许多人似乎认为 REST 的事情)是文章:我如何向我的妻子解释 REST。
As a high level concept, you should rely as little as possible not only on Session to store your state, but on statefulness in general in a web application. The idea is that the web itself is stateless by design, and when designing software on that paradigm the software should be designed to embrace a stateless nature.
More specifically, using a ViewModel gives you a strongly typed representation of the data needed for your view to pass down to the client. Pieces of that data which hold information about the state of a given request which can be made from that view can be added to the view in probably a number of ways, but the two most immediate ones are:
Check out the NerdDinner tutorial for a standard approach to using either ViewData or a strongly-typed ViewModel. Some Google searches will, as always, yield more information and tutorials. But note specifically where the tutorial uses the ViewModel properties in the view. These can be used anywhere in the rendering of the HTML, either in the HTML helpers or in manually constructing the tags.
Additional interesting reading regarding the statelessness of the web (and this whole not-as-new-as-many-people-seem-to-think REST thing) is the article: How I Explained REST to My Wife.