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


当前回答

ViewData

ViewData用于将数据从控制器传递给ViewData,它是从viewdatdictionary类派生出来的。仅对当前请求可用。仅要求复杂数据类型的类型转换,并检查null值以避免错误。如果发生重定向,则其值为null

ViewBag

ViewBag是一个动态属性,它利用了c# 4.0中的新动态特性。它也仅对当前请求可用。如果发生重定向,则其值为null

其他回答

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:对于复杂的数据类型,它不需要类型强制转换。

考虑下面的例子:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var emp = new Employee
        {
            EmpID=101,
            Name = "Deepak",
            Salary = 35000,
            Address = "Delhi"
        };

        ViewData["emp"] = emp;
        ViewBag.Employee = emp;

        return View(); 
    }
}

View的代码如下:

@model MyProject.Models.EmpModel;
@{ 
 Layout = "~/Views/Shared/_Layout.cshtml"; 
 ViewBag.Title = "Welcome to Home Page";
 var viewDataEmployee = ViewData["emp"] as Employee; //need type casting
}

<h2>Welcome to Home Page</h2>
This Year Best Employee is!
<h4>@ViewBag.Employee.Name</h4>
<h3>@viewDataEmployee.Name</h3>

我能建议你不要用吗?

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

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

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

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

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