我在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
其他回答
它使用c# 4.0动态特性。它实现了与viewdata相同的目标,应该避免使用强类型视图模型(与viewdata应该避免的方式相同)。
基本上它取代了魔法字符串:
ViewData["Foo"]
具有神奇属性:
ViewBag.Foo
你没有编译时安全性。
我继续指责微软在MVC中引入了这个概念。
属性的名称区分大小写。
public ActionResult Index()
{
ViewBag.Name = "Monjurul Habib";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Monjurul Habib";
return View();
}
In 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.
有一些细微的区别,这意味着你可以使用ViewData和ViewBag与视图略有不同的方式。这篇文章http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx中概述了一个优点,并展示了在示例中可以通过使用ViewBag而不是ViewData来避免强制转换。
这里ViewData和ViewBag都用于将数据从控制器传递到视图。
1. 显示数据
——ViewData是字典对象,从ViewDataDictonary类派生。
数据只允许一个请求,当页面重定向发生时ViewData值被清除。
——ViewData值必须在使用前输入。
示例:在控制器中
public ActionResult PassingDatatoViewWithViewData()
{
ViewData["Message"] = "This message shown in view with the ViewData";
return View();
}
在视图
@ViewData["Message"];
——ViewData是Key和Value这样的一对,Message是Key,在倒逗号中Value是Value。
数据是简单的,所以我们不能在这里使用类型转换,如果数据是复杂的,那么使用类型转换。
public ActionResult PassingDatatoViewWithViewData()
{
var type= new List<string>
{
"MVC",
"MVP",
"MVVC"
};
ViewData["types"] = type;
return View();
}
在视图中数据可以提取为
<ul>
@foreach (var items in (List<string>)ViewData["types"])
{
<li>@items</li>
}
</ul>
2. ViewBag
——ViewBag使用动态特性。ViewBag包装了ViewData。
—在ViewBag类型铸造是必需的。
——与ViewData相同,如果重定向发生,值将变为null。
例子:
public ActionResult PassingDatatoViewWithViewBag()
{
ViewData.Message = "This message shown in view with the ViewBag";
return View();
}
在视图
@ViewBag.vbMessage
—对于复杂类型使用ViewBag
public ActionResult PassingDatatoViewWithViewBag()
{
var type= new List<string>
{
"MVC",
"MVP",
"MVVC"
};
ViewBag.types = type;
return View();
}
在视图中数据可以提取为
<ul>
@foreach (var items in ViewBag.types)
{
<li>@items</li>
}
</ul>
主要的区别是ViewBag不需要类型转换,而ViewData需要类型转换。
推荐文章
- 如何从字符串中删除新的行字符?
- web页面的功能是什么:在MVC 3 web.config中启用
- 是否已经添加了事件处理程序?
- Lookup()和Dictionary(Of list()的区别
- 如何从我的c#代码运行EXE文件?
- Node.js vs .Net性能
- .NET控制台应用程序中的全局异常处理程序
- 枚举上最常见的c#位操作
- PowerShell脚本在机器上返回。net框架的版本?
- 用c#创建一个空文件
- 如何改变列表<T>数据IQueryable<T>数据
- 您可以使用反射来查找当前正在执行的方法的名称吗?
- 流。Seek(0, SeekOrigin.Begin)或Position = 0
- 如果查询为空,则最大返回值
- 传递一个实例化的系统。类型作为泛型类的类型参数