我在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"] 

其他回答

在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.)

这里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需要类型转换。

public ActionResult Index()
{
    ViewBag.Name = "Monjurul Habib";
    return View();
}

public ActionResult Index()
{
    ViewData["Name"] = "Monjurul Habib";
    return View();
} 

In View:

@ViewBag.Name 
@ViewData["Name"] 

它使用c# 4.0动态特性。它实现了与viewdata相同的目标,应该避免使用强类型视图模型(与viewdata应该避免的方式相同)。

基本上它取代了魔法字符串:

ViewData["Foo"]

具有神奇属性:

ViewBag.Foo

你没有编译时安全性。

我继续指责微软在MVC中引入了这个概念。

属性的名称区分大小写。

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"] 

详情请按此处: