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


当前回答

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>

其他回答

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>

有一些细微的区别,这意味着你可以使用ViewData和ViewBag与视图略有不同的方式。这篇文章http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx中概述了一个优点,并展示了在示例中可以通过使用ViewBag而不是ViewData来避免强制转换。

我能建议你不要用吗?

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

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

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

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