ViewDataDictionary Class
Represents a container that is used to pass data between a controller and a view.
Inheritance Hierarchy
System.Object
System.Web.Mvc.ViewDataDictionary
System.Web.Mvc.ViewDataDictionary<TModel>
Namespace: System.Web.Mvc
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
Syntax
'Declaration
Public Class ViewDataDictionary _
Implements IDictionary(Of String, Object), _
ICollection(Of KeyValuePair(Of String, Object)), IEnumerable(Of KeyValuePair(Of String, Object)), _
IEnumerable
public class ViewDataDictionary : IDictionary<string, Object>,
ICollection<KeyValuePair<string, Object>>, IEnumerable<KeyValuePair<string, Object>>,
IEnumerable
public ref class ViewDataDictionary : IDictionary<String^, Object^>,
ICollection<KeyValuePair<String^, Object^>>, IEnumerable<KeyValuePair<String^, Object^>>,
IEnumerable
The ViewDataDictionary type exposes the following members.
Constructors
Name | Description | |
---|---|---|
ViewDataDictionary() | Initializes a new instance of the ViewDataDictionary class. | |
ViewDataDictionary(Object) | Initializes a new instance of the ViewDataDictionary class by using the specified model. | |
ViewDataDictionary(ViewDataDictionary) | Initializes a new instance of the ViewDataDictionary class by using the specified dictionary. |
Top
Properties
Name | Description | |
---|---|---|
Count | Gets the number of elements in the collection. | |
IsReadOnly | Gets a value that indicates whether the collection is read-only. | |
Item | Gets or sets the item that is associated with the specified key. | |
Keys | Gets a collection that contains the keys of this dictionary. | |
Model | Gets or sets the model that is associated with the view data. | |
ModelMetadata | Gets or sets information about the model. | |
ModelState | Gets the state of the model. | |
TemplateInfo | Gets or sets an object that encapsulates information about the current template context. | |
Values | Gets a collection that contains the values in this dictionary. |
Top
Methods
Name | Description | |
---|---|---|
Add(KeyValuePair<String, Object>) | Adds the specified item to the collection. | |
Add(String, Object) | Adds an element to the collection using the specified key and value . | |
Clear | Removes all items from the collection. | |
Contains | Determines whether the collection contains the specified item. | |
ContainsKey | Determines whether the collection contains an element that has the specified key. | |
CopyTo | Copies the elements of the collection to an array, starting at a particular index. | |
Equals | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Eval(String) | Evaluates the specified expression. | |
Eval(String, String) | Evaluates the specified expression by using the specified format. | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetEnumerator | Returns an enumerator that can be used to iterate through the collection. | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
GetViewDataInfo | Returns information about the view data as defined by the expression parameter. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Remove(KeyValuePair<String, Object>) | Removes the first occurrence of a specified object from the collection. | |
Remove(String) | Removes the element from the collection using the specified key. | |
SetModel | Sets the data model to use for the view. | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
TryGetValue | Attempts to retrieve the value that is associated with the specified key. |
Top
Explicit Interface Implementations
Name | Description | |
---|---|---|
IEnumerable.GetEnumerator | Returns an enumerator that can be used to iterate through the collection. |
Top
Remarks
The ViewData property of a controller exposes an instance of the ViewDataDictionary class. To pass data to a view, you first add it to the controller's ViewData property in the action method that renders the view, as shown in the following example:
ViewData["color"] = "Red";
ViewData("color") = "Red"
When the view is rendered, the view data is copied to the ViewData property of the view. In the view markup, you can then access the data as shown in the following example:
<%= ViewData["color"] %>
<%= ViewData("color") %>
Similarly, you can pass data to the controller by adding the data to the ViewData property of the view, as shown in the following example:
<% ViewData["firstName"] = firstName %>
<% ViewData("firstName") = firstName %>
When the view is posted, its view data is sent to the controller, and you can then access the data in your action method as in the following example:
String firstName = ViewData["firstName"];
Dim firstName As String = ViewData("firstName")
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.