在没有会话或查询字符串的情况下在 MVC2 中保持状态的方法?

发布于 2024-10-11 19:58:32 字数 44 浏览 1 评论 0原文

有没有办法在 MVC2 中的回发之间保留信息而不使用会话变量或查询字符串?

Is there a way to keep information between post backs in MVC2 without utilizing Session variables or the query string?

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

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

发布评论

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

评论(5

爱*していゐ 2024-10-18 19:58:33

如果 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.

硪扪都還晓 2024-10-18 19:58:32

您的意思是像 .NET Web Forms 中的视图状态一样?从技术上讲,虽然不建议这样做,但最好利用模型并将模型数据发布到服务器并将模型推回到视图中。

这会很好地工作,但如果您需要像 WebForms ViewState 这样有状态的东西,我建议您在 WebForms 中完成您的项目或使用会话来保存您的模型。

编辑:构建将数据发布(或获取)回同一页面的表单。然后在你的控制器中,有一个这样的方法。

[HttpPost]
public ActionResult LoginUser(LoginViewModel model)
{
    //work on the model here
    return View(model);
}

这会将用户刚刚提交的表单数据推回到您的视图中。那么在你看来就有一个像这样的 Html 助手了。

<%: Html.TextboxFor(m => Model.Username) %>

网络上有大量关于将 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.

[HttpPost]
public ActionResult LoginUser(LoginViewModel model)
{
    //work on the model here
    return View(model);
}

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.

<%: Html.TextboxFor(m => Model.Username) %>

There are a slew of excellent resources on the web about using html helpers with models. Google around and you'll come across them.

安人多梦 2024-10-18 19:58:32

您可以使用隐藏表单字段在每次提交表单时将值 POST 回服务器。

You could use Hidden Form fields to POST the values back to the server with each form submission.

时光清浅 2024-10-18 19:58:32

其他替代方案包括 cookie 或 Http 缓存 - 是什么阻止您使用会话?

Other alternatives include a cookie or Http Cache - what is stopping you using session?

谈情不如逗狗 2024-10-18 19:58:32

作为一个高级概念,您不仅应该尽可能少地依赖 Session 来存储状态,而且还应该尽可能少地依赖 Web 应用程序中的状态性。这个想法是,网络本身在设计上是无状态的,并且在基于该范例设计软件时,软件应该被设计为包含无状态性质。

更具体地说,使用 ViewModel 为您提供视图传递给客户端所需的数据的强类型表示。保存有关可以从该视图发出的给定请求的状态信息的数据片段可能可以通过多种方式添加到视图中,但最直接的两个是:

  1. 作为隐藏表单字段元素
  2. 作为请求的 URL

查看 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:

  1. As hidden form field elements
  2. As parts of URLs for requests

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.

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