我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?
当前回答
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"]
详情请按此处:
其他回答
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
我注意到ViewData和ViewBag之间的一个主要区别是:
ViewData:它将返回object,不管你给它赋了什么,需要再次类型转换回原始类型。
ViewBag:它足够聪明,可以返回你分配给它的确切类型,不管你分配的是简单类型(即int,字符串等)还是复杂类型。
例如:控制器代码。
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
Products p1 = new Products();
p1.productId = 101;
p1.productName = "Phone";
Products p2 = new Products();
p2.productId = 102;
p2.productName = "laptop";
List<Products> products = new List<Products>();
products.Add(p1);
products.Add(p2);
ViewBag.Countries = products;
return View();
}
}
public class Products
{
public int productId { get; set; }
public string productName { get; set; }
}
}
视图代码。
<ul>
@foreach (WebApplication1.Controllers.Products item in ViewBag.Countries)
{
<li>@item.productId @item.productName</li>
}
</ul>
屏幕输出。
所有的答案都表明ViewBag和/或ViewData是将数据从控制器传递到视图,这是错误的信息。两者都非常有用,可以将数据从视图传递到布局或局部视图(或ViewComponents等)。它不是控制器专属的。
默认的asp.net示例在布局页面中有:
<title>@ViewData["Title"] - MyApp</title>
在任何情况下
ViewData["Title"] = "Details";
然后,问这个问题:“ViewBag和ViewData之间有什么区别?”
最显著的区别是ViewData是一个强类型字典 ViewBag是一个动态类型。
注意里面的数据IS the SAME
ViewData["Title"] = "MyTitle";
ViewBag.Title; // returns "MyTitle";
什么时候用这个或那个?
ViewBag doesn't support not valid C# names. you can't access ViewData["Key With Space"] with ViewBag ViewBag.Something is dynamic and you may have problems when calling methods (like extension methods) that needs to know the exact parameter at compile time. ViewBag can check for nulls syntactical cleaner: ViewBag.Person?.Name ViewData have all the properties of a Dictionary like ContainsKey, Add, etc. so you can use ViewData.Add("somekey", "somevalue") keep in mind it might throw exceptions. Using ViewData on views needs TypeCasting while ViewBag don't.
知道了细微的差别,使用其中一种更像是一种口味偏好。
通常你可以想到ViewBag。ViewData别名的任意键["AnyKey"]
ViewBag
它返回Type Object。 它是ControllerBase类的一个动态属性。 ViewBag仅适用于。net Framework 4.0及以上版本。 因为ViewBag属性本质上是动态的,所以在使用前不需要进行类型转换。 ViewBag返回动态类型对象,它的属性也是动态的。 它比ViewData快一点。
显示数据
它返回键值字典对集合。 ViewData是一个字典对象,它是ControllerBase类的属性。 ViewData比ViewBag快。 类型转换代码在枚举时是必需的,因为它是字典对集合。 ViewData返回对象(键值对的类型和值是类型对象,所以在使用前需要转换)
public ActionResult Index()
{
ViewBag.Name = "";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Arun Prakash";
return View();
}
视图呼叫
@ViewBag.Name
@ViewData["Name"]
下面是关于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.
推荐文章
- 为什么Func<T,bool>而不是Predicate<T>?
- 我如何能使一个组合框不可编辑的。net ?
- .NET反射的成本有多高?
- 将流转换为字符串并返回
- IEquatable和重写Object.Equals()之间的区别是什么?
- 创建一个堆栈大小为默认值50倍的线程有什么危险?
- 选择Enum类型的默认值而无需更改值
- String与StringBuilder
- 远程主机强制关闭现有连接
- 为什么ReSharper想要使用'var'的一切?
- 如何检查一个对象是否为空?
- 删除后面的零
- 从c#读取Excel文件
- 未将相关DLL复制到Visual Studio中的构建输出文件夹
- 存储值为> 24:00:00的.Net Timespan的正确SQL类型是什么?