在asp.net mvc的状态管理ViewBag和ViewData的一部分也? [英] Is ViewBag and ViewData also part of state management in asp.net mvc?
问题描述
有人可以告诉我这是否的ViewData strong>和 ViewBag 也与否的 asp.net mvc的状态管理的一部分?
谢谢
Can somebody please tell me that whether ViewData and ViewBag are also part of asp.net mvc state management or not? Thanks
推荐答案
ViewBag
和的ViewData code>不存储状态的,但他们可以将它传递给次要呈现和存储
ViewBag
and ViewData
do not store state, but they can pass it to views to be rendered and stored.
ViewBag
和的ViewData code>未注明的持久的机制,但我相信他们是状态管理的一部分:它们是将数据传递到随后可以持续在生成的HTML状态的页面的机制。这样,他们是国家的生命周期的一部分,因为它们允许你使用佣工存储在您的客户端HTML状态,如
@ Html.HiddenFor
或 @ Html.ActionLink
。
ViewBag
and ViewData
are not state persistence mechanisms, but I believe they are part of State Management: they are mechanisms for passing data to a page which can then be persisted as state in the generated html. In this way they are part of the lifecycle of state, as they allow you to store state in your client side html using helpers such as @Html.HiddenFor
or @Html.ActionLink
.
在我的回答存储1的ActionResult一个JS值在其他的ActionResult使用我谈谈如何 ViewBag
和的ViewData code>可用于存储在客户端HTML状态,什么状态存储的各种选项。
In my answer to "storing a js value from 1 ActionResult to use in another ActionResult" I talk about how ViewBag
and ViewData
can be used to store state in the client html, and what the various options for state storage are.
至于什么 ViewBag
是,它实际上是一个动态访问
的ViewData <中
办法/ code>,所以 ViewBag.MyItem =富;
和 VAR valueEqualsFoo =计算机[MyItem];
将设置并返回相同的字符串,可以互换。
As for what ViewBag
is, it's actually a dynamic
way of accessing ViewData
, so ViewBag.MyItem = "foo";
and var valueEqualsFoo = ViewData["MyItem"];
will set and return the same string and can be interchanged.
ViewBag
,的ViewData code>最密切连接到一个Action视图模型,在该模型被传递到使用
行动内的一个视图返回视图(视图模型);
:所有三种技术通过国家在内存中进入它被发送到客户端的HTML,任何中间缓存,从您的服务器坚持了。
ViewBag
, ViewData
are most closely connected to a View Model in an Action, where the model is passed to a View inside the Action using return View(viewModel);
: all three techniques pass the state in memory into the html where it is sent to the client, to any intermediate caches, and "persisted" away from your server.
在一个类似的方式,当在一个URL的查询串中一个http请求被发送到服务器它是一个方法的传递的状态下,在实际状态存储的是&LT; A HREF =urlwithquerystring&GT; ...&LT; / A&gt;在HTML
主播。宁静的URL和机构的POST Ajax请求,在他们的定义和行为是一样的。 ViewBag /数据
从行动到HTML,它被传递到客户端和存储通过国家,查询字符串或宁静的网址,然后通过国家返回给服务器在接下来的行动调用使用。
In a similar way, when a query string in a url is sent to a server in an http request it is a method of passing state, the actual state store is the <a href="urlwithquerystring">...</a>
anchor in the html. Restful URLs, and bodies for POST ajax requests, are the same in their definition and behaviour. ViewBag/Data
pass the state from the Action to the html, which is passed to the client and stored, the query string or restful url then pass the state back to the server for use in the next Action invocation.
这是很难检查与您的剃刀code拼写错误的动态性能;很容易检查,如果在强类型视图模型存在的属性。因此,我相信你应该很少使用它们。在我看来,这是preferable创建强类型的视图模型,而不是使用 ViewBag
或的ViewData code>。他们的可能的被罚款的<一个href=\"http://programmers.stackexchange.com/questions/124835/how-do-quick-dirty-programmers-know-they-got-it-right\">quick和肮脏的解决方案,但这些东西往往会创造技术债务。
ViewBag
是的可能的确定为设置页面的标题。
It is hard to check for dynamic properties with a misspelling in your Razor code; it is easy to check if a property exists on a strongly typed view model. Therefore I believe you should use them very rarely. In my opinion it is preferable to create strongly typed view models rather than use ViewBag
or ViewData
. They might be fine for a quick and dirty solution, but those things tend to create Technical Debt. ViewBag
is possibly ok for setting the page title.
强类型的视图模型:
- 请它更容易使用的映射与像 automapper 框架;
- 请访问量更可检验的;和
- 使其更容易创造,你传递相同的模型,每个视图不同设备不同的看法&匕首;
- make it easier to use mapping with frameworks like automapper;
- make views more testable; and
- make it easier to create different views for different devices where you pass the same model to each view †
我说这里,我再说一遍:过去有至少的管理永久性用户状态的许多那些http://msdn.microsoft.com/en-us/magazine/cc300437.aspx\">Nine选项,还适用于MVC。他们都有不同的用途取决于如何的状态应该被使用。一般服务器端code是尽可能无状态更容易测试和调试。
I said it here, and I'll say it again: There used to be at least Nine Options for Managing Persistent User State in ASP.NET, and many of those still apply in MVC. They all have different uses depending on how the state should be used. Generally server side code that is as stateless as possible is easier to test and debug.
- 客户端的存储,包括:
- 实体传递回HTML的结果,变成了:
- 输入域
- 隐藏字段
- 的Javascript变量用于Ajax请求POST机构
- 查询字符串值,并在网址路径(如宁静的网址,如
/产品/ 1
)
- Client side storage includes:
- Entities passed back in the html result, and turned into:
- Input fields
- Hidden Fields
- Javascript variables for use in ajax request POST bodies
- Query string values, and paths in URLs (e.g. Restful URLs like
/Product/1
)
- 对话(可通过控制器的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.session%28v=vs.108%29.aspx\"><$c$c>Session$c$c>财产
- 缓存(可通过控制器的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.session%28v=vs.108%29.aspx\"><$c$c>Cache$c$c>属性)
- TempData
- 的HttpContext [项目的关键]
- Application (可通过控制器的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext%28v=vs.108%29.aspx\"><$c$c>HttpContext$c$c>财产
HttpContext.Application
) - 的Web.config和Machine.config中的文件(是的,这也是保持状态,那只是应用宽),因此,
WebConfigurationManager.AppSettings
词典。
- Session (available through the Controller's
Session
property - Cache (available through the Controller's
Cache
property) - TempData
- HttpContext["item key"]
- Application (available through the Controller's
HttpContext
property asHttpContext.Application
) - Web.config and Machine.config files (yes, these also hold state, it is just application wide) and therefore the
WebConfigurationManager.AppSettings
dictionary.
- 数据库是否SQL或NOSQL
- 文件存储
- 消息队列,如WASB
的脚注:的
&匕首; 的我们现在有易于使用的工具,响应式设计我们可以使用时 适当,但它是并不总是合适的:一些看法需要看看在移动一个完全不同>但仍然使用相同的视图模型作为大屏幕网站的。
† We now have easy to use tools for responsive design which we can use when appropriate, but it is not always suitable: some views need to look entirely different on mobile but still use the same viewmodel as the large screen site.
这篇关于在asp.net mvc的状态管理ViewBag和ViewData的一部分也?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Entities passed back in the html result, and turned into:
- 实体传递回HTML的结果,变成了: