加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – VIEWDATA和VIEWBAG存储在MVC中的哪个位置?

发布时间:2020-12-15 19:03:22 所属栏目:asp.Net 来源:网络整理
导读:我对MVC非常新…在ASP .Net中有状态管理技术,其中viewstate或cookie存储在客户端和会话存储在服务器中.类似地,我们在MVC中有Viewbag,ViewData和TempData(cookie和会话也在那里).我知道来自控制器ViewData的语法存储为 ViewData[“Foo”] = “bar”; ViewBag.
我对MVC非常新…在ASP .Net中有状态管理技术,其中viewstate或cookie存储在客户端和会话存储在服务器中.类似地,我们在MVC中有Viewbag,ViewData和TempData(cookie和会话也在那里).我知道来自控制器ViewData的语法存储为

ViewData[“Foo”] = “bar”;

ViewBag.Foo = “bar”;

在相应的视图中,它被取为

ViewData[“Foo”] = “bar”;

@ViewBag.Foo

我想知道的是ViewData和ViewBag存储在哪里(客户端或服务器或其他地方)?
请原谅我,如果这是一个无关紧要的问题,

解决方法

ViewBag和 ViewData是国家管理的一部分.它们都是允许(主要)从Controller传递到View的数据.

这完全发生在服务器端,但数据“存储”在服务器上的想法是误导性的.这些是瞬态对象,仅在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应用程序中,模型应该是传递给视图而不是其他任何东西的东西.过度使用ViewBag和ViewData是poor practice.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读