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
Public method ViewDataDictionary() Initializes a new instance of the ViewDataDictionary class.
Public method ViewDataDictionary(Object) Initializes a new instance of the ViewDataDictionary class by using the specified model.
Public method ViewDataDictionary(ViewDataDictionary) Initializes a new instance of the ViewDataDictionary class by using the specified dictionary.

Top

Properties

  Name Description
Public property Count Gets the number of elements in the collection.
Public property IsReadOnly Gets a value that indicates whether the collection is read-only.
Public property Item Gets or sets the item that is associated with the specified key.
Public property Keys Gets a collection that contains the keys of this dictionary.
Public property Model Gets or sets the model that is associated with the view data.
Public property ModelMetadata Gets or sets information about the model.
Public property ModelState Gets the state of the model.
Public property TemplateInfo Gets or sets an object that encapsulates information about the current template context.
Public property Values Gets a collection that contains the values in this dictionary.

Top

Methods

  Name Description
Public method Add(KeyValuePair<String, Object>) Adds the specified item to the collection.
Public method Add(String, Object) Adds an element to the collection using the specified key and value .
Public method Clear Removes all items from the collection.
Public method Contains Determines whether the collection contains the specified item.
Public method ContainsKey Determines whether the collection contains an element that has the specified key.
Public method CopyTo Copies the elements of the collection to an array, starting at a particular index.
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Eval(String) Evaluates the specified expression.
Public method Eval(String, String) Evaluates the specified expression by using the specified format.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Returns an enumerator that can be used to iterate through the collection.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetViewDataInfo Returns information about the view data as defined by the expression parameter.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Remove(KeyValuePair<String, Object>) Removes the first occurrence of a specified object from the collection.
Public method Remove(String) Removes the element from the collection using the specified key.
Protected method SetModel Sets the data model to use for the view.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryGetValue Attempts to retrieve the value that is associated with the specified key.

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method 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.

See Also

Reference

System.Web.Mvc Namespace

Other Resources

Views and UI Rendering in ASP.NET MVC Applications

Passing Data in an ASP.NET MVC Application