ViewDataDictionary Class
Represents a container that is used to pass data between a controller and a view.
Namespace: System.Web.Mvc
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
The ViewDataDictionary type exposes the following members.
| 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. |
| 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. |
| 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 | (Inherited from Object.) |
![]() | Eval(String) | Evaluates the specified expression. |
![]() | Eval(String, String) | Evaluates the specified expression by using the specified format. |
![]() | Finalize | (Inherited from Object.) |
![]() | GetEnumerator | Returns an enumerator that can be used to iterate through the collection. |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() | GetViewDataInfo | Returns information about the view data as defined by the expression parameter. |
![]() | MemberwiseClone | (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 | (Inherited from Object.) |
![]() | TryGetValue | Attempts to retrieve the value that is associated with the specified key. |
| Name | Description | |
|---|---|---|
![]() ![]() | IEnumerable.GetEnumerator | Returns an enumerator that can be used to iterate through the collection. |
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:
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:
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:
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:
.gif?cs-save-lang=1&cs-lang=fsharp)
.gif?cs-save-lang=1&cs-lang=fsharp)
.gif?cs-save-lang=1&cs-lang=fsharp)
.gif?cs-save-lang=1&cs-lang=fsharp)
.gif?cs-save-lang=1&cs-lang=fsharp)