我在MVC 3中看到了ViewBag。这和MVC 2中的ViewData有什么不同?


当前回答

在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:是一个动态属性,在它的工作类似于视图数据,但它是更好的,因为它不需要在视图中使用它之前被强制转换为相应的模型…

有一些细微的区别,这意味着你可以使用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之间的一个主要区别是:

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 &nbsp;&nbsp;&nbsp;@item.productName</li>
            }
        </ul>

屏幕输出。

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

我能建议你不要用吗?

如果你想“发送”数据到你的屏幕上,发送一个强类型对象(又名ViewModel),因为它更容易测试。

如果你绑定到某种类型的“模型”,并拥有随机的“viewbag”或“viewdata”项,那么这会使自动化测试变得非常困难。

如果您正在使用这些,请考虑如何重构并仅使用ViewModels。