asp.net-mvc - VIEWDATA 和 VIEWBAG 在 MVC 中存储在哪里?

标签 asp.net-mvc

我对 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 存储在哪里(客户端或服务器或其他地方)?
如果是无关紧要的问题,请见谅,,,,,

最佳答案

ViewBagViewData是状态管理的一部分。它们都是允许将数据(主要)从 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/

相关文章:

asp.net-mvc - 使用 ASP.NET Web API 进行跨平台身份验证

c# - Asp.net MVC 中的日期时间选择器中未设置日期

asp.net - Controller 创建时无法检索元数据

javascript - 防止MVC中的ajax调用

asp.net-mvc - ASP.NET MVC ActionLink 与 URL 重写

c# - 移动设备 _Layout.Mobile.cshtml 和 iPad

asp.net-mvc - asp net mvc出现500错误,页面呈现unicode

c# - 如何通过 DataReader/DataTable 来查看?

javascript - 使用 JavaScript/jQuery,如何根据下拉列表中的选择过滤列表?

asp.net-mvc - 多重性与角色中的参照约束冲突