在asp.net mvc的状态管理ViewBag和ViewData的一部分也? [英] Is ViewBag and ViewData also part of state management in asp.net mvc?

查看:130
本文介绍了在asp.net mvc的状态管理ViewBag和ViewData的一部分也?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我这是否的ViewData 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 不存储状态的,但他们可以将它传递给次要呈现和存储

ViewBag and ViewData do not store state, but they can pass it to views to be rendered and stored.

ViewBag 的ViewData 未注明的持久的机制,但我相信他们是状态管理的一部分:它们是将数据传递到随后可以持续在生成的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 可用于存储在客户端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 最密切连接到一个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 。他们的可能的被罚款的<一个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.

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