Export (0) Print
Expand All

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)

public class ViewDataDictionary implements IDictionary<String, Object>, ICollection<KeyValuePair<String, Object>>, IEnumerable<KeyValuePair<String, Object>>, IEnumerable

The ViewDataDictionary type exposes the following members.

  NameDescription
Public methodViewDataDictionary()Initializes a new instance of the ViewDataDictionary class.
Public methodViewDataDictionary(Object)Initializes a new instance of the ViewDataDictionary class by using the specified model.
Public methodViewDataDictionary(ViewDataDictionary)Initializes a new instance of the ViewDataDictionary class by using the specified dictionary.
Top

  NameDescription
Public propertyCountGets the number of elements in the collection.
Public propertyIsReadOnlyGets a value that indicates whether the collection is read-only.
Public propertyItemGets or sets the item that is associated with the specified key.
Public propertyKeysGets a collection that contains the keys of this dictionary.
Public propertyModelGets or sets the model that is associated with the view data.
Public propertyModelMetadataGets or sets information about the model.
Public propertyModelStateGets the state of the model.
Public propertyTemplateInfoGets or sets an object that encapsulates information about the current template context.
Public propertyValuesGets a collection that contains the values in this dictionary.
Top

  NameDescription
Public methodAdd(KeyValuePair(String, Object))Adds the specified item to the collection.
Public methodAdd(String, Object)Adds an element to the collection using the specified key and value .
Public methodClearRemoves all items from the collection.
Public methodContainsDetermines whether the collection contains the specified item.
Public methodContainsKeyDetermines whether the collection contains an element that has the specified key.
Public methodCopyToCopies the elements of the collection to an array, starting at a particular index.
Public methodEquals (Inherited from Object.)
Public methodEval(String)Evaluates the specified expression.
Public methodEval(String, String)Evaluates the specified expression by using the specified format.
Protected methodFinalize (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that can be used to iterate through the collection.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodGetViewDataInfoReturns information about the view data as defined by the expression parameter.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodRemove(KeyValuePair(String, Object))Removes the first occurrence of a specified object from the collection.
Public methodRemove(String)Removes the element from the collection using the specified key.
Protected methodSetModelSets the data model to use for the view.
Public methodToString (Inherited from Object.)
Public methodTryGetValueAttempts to retrieve the value that is associated with the specified key.
Top

  NameDescription
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an enumerator that can be used to iterate through the collection.
Top

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:

No code example is currently available or this language may not be supported.

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:

No code example is currently available or this language may not be supported.

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:

No code example is currently available or this language may not be supported.

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:

No code example is currently available or this language may not be supported.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft