Export (0) Print
Expand All

ViewDataDictionary Class

.NET Framework 4

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 : 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 methodEqualsDetermines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public methodEval(String)Evaluates the specified expression.
Public methodEval(String, String)Evaluates the specified expression by using the specified format.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that can be used to iterate through the collection.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetViewDataInfoReturns information about the view data as defined by the expression parameter.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (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 methodToStringReturns a string that represents the current object. (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:

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"] %>

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 %>

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"];

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

Community Additions

ADD
Show:
© 2015 Microsoft