我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?
当前回答
Although you might not have a technical advantage to choosing one format over the other, you should be aware of some important differences between the two syntaxes. One obvious difference is that ViewBag works only when the key you’re accessing is a valid C# identifi er. For example, if you place a value in ViewData["Key With Spaces"], you can’t access that value using ViewBag because the code won’t compile. Another key issue to consider is that you cannot pass in dynamic values as parameters to extension methods. The C# compiler must know the real type of every parameter at compile time in order to choose the correct extension method. If any parameter is dynamic, compilation will fail. For example, this code will always fail: @Html.TextBox("name", ViewBag.Name). To work around this, either use ViewData["Name"] or cast the va
其他回答
在ViewBag内部,属性以名称/值对的形式存储在ViewData字典中。
注意:在MVC 3的大多数预发布版本中,ViewBag属性被命名为ViewModel,这段代码来自MVC 3发布说明:
有人建议我发布我发布的这个信息的来源,下面是来源: http://www.asp.net/whitepapers/mvc3-release-notes#_Toc2_4
MVC 2 controllers support a ViewData property that enables you to pass data to a view template using a late-bound dictionary API. In MVC 3, you can also use somewhat simpler syntax with the ViewBag property to accomplish the same purpose. For example, instead of writing ViewData["Message"]="text", you can write ViewBag.Message="text". You do not need to define any strongly-typed classes to use the ViewBag property. Because it is a dynamic property, you can instead just get or set properties and it will resolve them dynamically at run time. Internally, ViewBag properties are stored as name/value pairs in the ViewData dictionary. (Note: in most pre-release versions of MVC 3, the ViewBag property was named the ViewModel property.)
ViewBag和ViewData是ASP中用来将信息从控制器传递到视图的两种方式。净MVC。使用这两种机制的目的是提供控制器和视图之间的通信。两者的生命周期都很短,即一旦重定向发生,两者的值都将变为null,即一旦页面从源页面(我们在其中设置了ViewBag或ViewData的值)重定向到目标页面,ViewBag和ViewData都将变为null。
尽管ViewBag和ViewData有这些相似之处,但如果我们讨论它们的实现,它们是两个不同的东西。区别如下:
1)。如果我们分析这两个实现,我们会发现ViewData是一个字典数据结构——对象字典从viewdatdictionary派生而来,可以使用字符串作为这些值的键来访问,而ViewBag利用了c# 4.0中引入的动态特性,并且是一个动态属性。
2)。当从ViewData中访问值时,我们需要类型转换值(数据类型),因为它们存储为ViewData字典中的对象,但如果我们在ViewBag中访问值,则不需要这样做。
3)。在ViewBag中,我们可以像这样设置值:
ViewBag.Name = "Value";
并可访问如下:
@ViewBag.Name
而在ViewData的情况下,值可以设置和访问如下: 设置ViewData的方法如下:
ViewData["Name"] = "Value";
像这样获取价值
@ViewData["Name"]
详情请按此处:
我能建议你不要用吗?
如果你想“发送”数据到你的屏幕上,发送一个强类型对象(又名ViewModel),因为它更容易测试。
如果你绑定到某种类型的“模型”,并拥有随机的“viewbag”或“viewdata”项,那么这会使自动化测试变得非常困难。
如果您正在使用这些,请考虑如何重构并仅使用ViewModels。
通过这种方式,我们可以让它使用这些值来将控制器之间的信息传递到带有TEMP DATA的其他页面
下面是关于ViewData, ViewBag, TempData和Session的点对点差异。 信贷/ askforprogram复制。在这里,遵循我没有提到的代码示例的链接。
ViewData in MVC ViewData is property of ControllerBase class. ViewData is a type of dictionary object. ViewData is key-value dictionary collection. ViewData was introduced in MVC 1.0 version. ViewData works with .Net framework 3.5 and above. Need to do type conversion of code while enumerating. ViewData object keeps data only for current request. ViewBag in MVC ViewBag is property of ControllerBase class. ViewBag is a type of dynamic object. ViewBag is a type of object. ViewBag was introduced in MVC 3.0 version. ViewBag works with .Net framework 4.0 and above. ViewBag uses property and handles it, so no need to do type conversion while enumerating. ViewBag object keeps data only for current request. TempData in MVC TempData is property of ControllerBase class. TempData is a type of dictionary object. TempData is key-value dictionary collection. TempData was introduced in MVC 1.0 version. TempData works with .Net framework 3.5 and above. Need to do type conversion of code while enumerating. TempData object is used to data between current request and subsequent request. Session in MVC Session is property of Controller(Abstract Class). Session is a type of HttpSessionStateBase. Session is key-value dictionary collection. Session was introduced in MVC 1.0 version. TempData works with .Net framework 1.0 and above. Need to do type conversion of code while enumerating. Session object keeps data for all requests. Valid for all requests, never expires.
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- c# .NET中的App.config是什么?如何使用它?
- String类中的什么方法只返回前N个字符?
- 我如何提高ASP。NET MVC应用程序性能?
- 无法解析类型为“Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务
- 如何在没有任何错误或警告的情况下找到构建失败的原因
- Visual Studio弹出提示:“操作无法完成”
- ViewBag, ViewData和TempData
- 否ConcurrentList<T>在。net 4.0?
- 在ASP中设置Access-Control-Allow-OriginNet MVC -最简单的方法
- 在c#中解析字符串为日期时间
- 由Jon Skeet撰写的《Singleton》澄清
- 自定义数字格式字符串始终显示符号
- Post参数始终为空