我对 MVC 非常陌生......在 ASP .Net 中有状态管理技术,其中 View 状态或 cookie 存储在客户端和 session 存储在服务器中。类似地,我们在 MVC 中有 Viewbag、ViewData 和 TempData(cookies 和 session 也在那里)。我知道 Controller ViewData 存储为的语法
ViewData["Foo"] = "bar";
ViewBag.Foo = "bar";
在相应的 View 中,它被提取为
ViewData["Foo"] = "bar";
@ViewBag.Foo
我只想知道 ViewData 和 ViewBag 存储在哪里(客户端或服务器或其他地方)?
如果是无关紧要的问题,请见谅,,,,,
最佳答案
ViewBag和 ViewData是状态管理的一部分。它们都是允许将数据(主要)从 Controller 传递到 View 的对象。
这完全发生在服务器端,但数据“存储”在服务器上的想法是误导性的。这些是 transient 对象,仅在 HTTP 请求的生命周期内有效。
ViewBag 和 ViewData 的用例是:
transporting small amounts of data from and to specific locations (e.g., controller to view or between views). Both the ViewData and ViewBag objects work well in the following scenarios:
- Incorporating dropdown lists of lookup data into an entity
- Components like a shopping cart
- Widgets like a user profile widget
- Small amounts of aggregate data
来自 http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
要避免的一件事是过度使用 ViewBag/ViewData。在 MVC 应用程序中,模型应该是传递给 View 的东西,而不是其他任何东西。过度使用 ViewBag 和 ViewData 是一个 poor practice .
关于asp.net-mvc - VIEWDATA 和 VIEWBAG 在 MVC 中存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157886/