Part 3 ViewData and ViewBag in mvc

ViewBag and ViewData is a mechanism(机制) to pass data from controller to view.

We use '@' symbol(符号) to switch between html and C# code.

Both ViewData and ViewBag are used to pass data from a controller to a view.

ViewData is a dictionary of object that are stored and retrieved(取回) using strings as keys.

ViewBag uses the dynamic feature that was introduced in to C#4. It allows an object to have properties dynamically added to it.

Both ViewData and ViewBag does not provide compile time error checking. For example, if you mis-spll keys or property names, you wouldn't get any compile time error. You get to know about the error only at runtime. Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.

原文地址:https://www.cnblogs.com/gester/p/4856154.html