与 ASP.NET MVC 中的 ViewData 等效吗?

发布于 2024-08-11 05:17:24 字数 304 浏览 1 评论 0原文

ASP.NET MVC 中是否有与 ASP.NET 的 ViewData/ TempData 对象等效的对象?

我想要做的就是让会话项仅针对一个请求保持活动状态。

提前致谢!

编辑:

问题是我只有一个用于更新和创建的视图。当视图处于更新模式时,会话项将被填充,或者必须已经填充(!),而在创建模式下,会话项为空。因此,当我处于创建模式并将页面发送回服务模式时,模式更改为编辑模式,并且视图上的按钮被启用,因为会话项已填充。该模式取决于会话项目。并且会话项可以由另一个视图或当我成功创建联系人时填充。会话项值是 contactId。

Is there an equivalent for the ViewData/ TempData object in ASP.NET MVC for ASP.NET?

What I wanna do is to keep a Session item only alive for one request.

Thanks in advance!

Edit:

The problem is that I have one view for updating and creating. When the view is in update mode then the session item is filled or it has to be already filled(!) and in the create mode the session item is null. So when I am in creating mode and send the page back to serve the mode changed into edit mode and a button on the view is enabled because the session item was filled. The mode depends on the session item. And the Session item can be filled by another view or when I create a contact successfully. The Session item value is the contactId.

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

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

发布评论

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

评论(3

北笙凉宸 2024-08-18 05:17:24

ViewState 是最接近的。它是一个页面范围的持久性存储,但它可以在对同一页面的许多后续请求中幸存下来。

您可以根据您的需要进行调整。在页面构造函数中初始化一些 RequestID 值,并将其保留在 Session 中,与您需要的 Session 变量紧密耦合。从会话中读取时,您可以简单地检查标识符是否指向当前请求或上一代的请求。这或多或少是 TempData 的实现方式。

您还可以查看 RequestData 集合。但它仅在一个请求期间保留,不会延伸到下一个请求。

ViewState is the closest you can have. it is a page-scope persistence storage but it will survive many subsequent requests to the same page.

You can adapt it to your needs. Initialize some RequestID value in page constructor and keep it in the Session tightly coupled to the Session variable you need. When reading from session, you can simply check if the identifier points to the current request or to the one of some previous generation. It's more or less how the TempData is implemented.

You can also take a look at RequestData collection. It is though kept for the duration of one request only and is not stretched to the next request.

書生途 2024-08-18 05:17:24

只为了一个请求?连回传都没有吗?然后你可以只使用受保护的字段或属性。

For just one request? Not even a postback? Then you could just use a protected field or property.

旧城空念 2024-08-18 05:17:24

您可以使用 request 对象。您可能还会发现页面很有帮助。

You can use request object. You might also find this page helpful.

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