我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?
当前回答
public ActionResult Index()
{
ViewBag.Name = "Monjurul Habib";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Monjurul Habib";
return View();
}
In View:
@ViewBag.Name
@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
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"]
public ActionResult Index()
{
ViewBag.Name = "Monjurul Habib";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Monjurul Habib";
return View();
}
In View:
@ViewBag.Name
@ViewData["Name"]
ViewBag vs MVC中的ViewData
http://royalarun.blogspot.in/2013/08/viewbag-viewdata-tempdata-and-view.html
ViewBag和ViewData的相似之处:
当您从控制器移动到视图时,有助于维护数据。用于 将数据从控制器传递到相应的视图。短寿命意味着 值在重定向发生时变为空。这是因为他们的目标 是提供一种在控制器和视图之间通信的方法。这是 服务器调用中的通信机制。
ViewBag和ViewData的区别:
ViewData是派生自的对象字典 viewdatdictionary类,并使用字符串作为键访问。ViewBag 动态属性是否利用了新的动态特性 在c# 4.0。ViewData需要对复杂的数据类型进行类型转换 检查空值以避免错误。ViewBag不需要 复杂数据类型的类型转换。
ViewBag & ViewData示例:
public ActionResult Index()
{
ViewBag.Name = "Arun Prakash";
return View();
}
public ActionResult Index()
{
ViewData["Name"] = "Arun Prakash";
return View();
}
视图呼叫
@ViewBag.Name
@ViewData["Name"]
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"]
详情请按此处:
推荐文章
- 如何从字符串中删除新的行字符?
- 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
- 如果查询为空,则最大返回值
- 传递一个实例化的系统。类型作为泛型类的类型参数