在哪里可视数据和VIEWBAG存储在MVC? [英] Where are VIEWDATA and VIEWBAG stored in MVC?

查看:233
本文介绍了在哪里可视数据和VIEWBAG存储在MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的MVC ...在ASP.NET的存在在那里的ViewState或Cookie存储在客户端和会话存储在服务器上的状态管理技术。同样,我们有Viewbag,ViewData的和TempData的MVC中(cookie和session也存在)。我知道的语法就像从控制器的ViewData存储为

i am very much new to MVC...In ASP .Net there was state management technique where viewstate or cookies were stored in client and session stored in server. Similarly we have Viewbag,ViewData and TempData in MVC(cookies and sessions are also there).I know the syntaxes like from controller ViewData stored as

计算机[富] =酒吧;

ViewData["Foo"] = "bar";

ViewBag.Foo =酒吧;

ViewBag.Foo = "bar";

和在相应的视图是牵强,因为

and in the corresponding view it is fetched as

计算机[富] =酒吧;

ViewData["Foo"] = "bar";

@ ViewBag.Foo

@ViewBag.Foo

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

All I want to know is that where are ViewData and ViewBag stored(client or server or someplace else)?? Please forgive me if it is a irrelevant question,,,,,

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag(v=vs.118).aspx\">ViewBag和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.viewdata(v=vs.118).aspx\">ViewData是国家管理的一部分。他们是允许从控制器传递数据(主要),以查看这两个对象。

ViewBag and ViewData are part of state management. They are both objects that allow passing of data (mainly) from the Controller to the View.

这完全是发生在服务器端,但该数据在服务器上的存储的理念是一种误导。这些都是瞬时对象只活在HTTP请求的生命周期。

This happens entirely on the server side but the idea that the data is "stored" on the server is misleading. These are transient objects which only live for the lifetime of the HTTP request.

使用案例ViewBag和ViewData的是:

The use case for ViewBag and ViewData is:

输送少量从和到特定位置(例如,控制器,以查看或次之间)的数据。无论是ViewData的和ViewBag对象在下列情况下工作良好:

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:


      
  • 纳入查找数据的下拉列表到实体

  •   
  • 组件就像一个购物车

  •   
  • 窗口小部件如用户配置文件部件

  •   
  • 少量的汇总数据

  •   

从<一个href=\"http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications\">http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

有一件事要尽量避免过度使用是ViewBag / ViewData的。在MVC应用程序的模式应该是传递给视图,而不是别的什么东西。 ViewBag和ViewData的过度使用是一个差实践

One thing to try and avoid is overusing ViewBag / ViewData. In a MVC application the model should be the thing that is passed to the view rather than anything else. Overuse of ViewBag and ViewData is a poor practice.

这篇关于在哪里可视数据和VIEWBAG存储在MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆