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)
Syntax
Public Class ViewDataDictionary _
	Implements [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_0%](Of [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_1%], [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_2%]),  _
	[%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_3%](Of [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_4%](Of [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_5%], [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_6%])), [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_7%](Of [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_8%](Of [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_9%], [%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_10%])),  _
	[%$TOPIC/dd493055_en-us_VS_118_2_0_0_0_11%]
Dim instance As ViewDataDictionary
public class ViewDataDictionary : [%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_0%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_1%], [%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_2%]>, 
	[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_3%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_4%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_5%], [%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_6%]>>, [%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_7%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_8%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_9%], [%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_10%]>>, 
	[%$TOPIC/dd493055_en-us_VS_118_2_0_2_0_11%]
public ref class ViewDataDictionary : [%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_0%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_1%]^, [%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_2%]^>, 
	[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_3%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_4%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_5%]^, [%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_6%]^>>, [%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_7%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_8%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_9%]^, [%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_10%]^>>, 
	[%$TOPIC/dd493055_en-us_VS_118_2_0_3_0_11%]
type ViewDataDictionary =  
    class 
        interface [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_0%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_1%], [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_2%]>
        interface [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_3%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_4%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_5%], [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_6%]>>
        interface [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_7%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_8%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_9%], [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_10%]>>
        interface [%$TOPIC/dd493055_en-us_VS_118_2_0_4_0_11%] 
    end
public class ViewDataDictionary implements [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_0%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_1%], [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_2%]>, [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_3%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_4%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_5%], [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_6%]>>, [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_7%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_8%]<[%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_9%], [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_10%]>>, [%$TOPIC/dd493055_en-us_VS_118_2_0_5_0_11%]

The ViewDataDictionary type exposes the following members.

Constructors
  NameDescription
Public method ViewDataDictionaryInitializes 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
  NameDescription
Public property CountGets the number of elements in the collection.
Public property IsReadOnlyGets a value that indicates whether the collection is read-only.
Public property ItemGets or sets the item that is associated with the specified key.
Public property KeysGets a collection that contains the keys of this dictionary.
Public property ModelGets or sets the model that is associated with the view data.
Public property ModelMetadataGets or sets information about the model.
Public property ModelStateGets the state of the model.
Public property TemplateInfoGets or sets an object that encapsulates information about the current template context.
Public property ValuesGets a collection that contains the values in this dictionary.
Top
Methods
  NameDescription
Public method Add(KeyValuePairString, 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 ClearRemoves all items from the collection.
Public method ContainsDetermines whether the collection contains the specified item.
Public method ContainsKeyDetermines whether the collection contains an element that has the specified key.
Public method CopyToCopies the elements of the collection to an array, starting at a particular index.
Public method Equals (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 (Inherited from Object.)
Public method GetEnumeratorReturns an enumerator that can be used to iterate through the collection.
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method GetViewDataInfoReturns information about the view data as defined by the expression parameter.
Protected method MemberwiseClone (Inherited from Object.)
Public method Remove(KeyValuePairString, 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 SetModelSets the data model to use for the view.
Public method ToString (Inherited from Object.)
Public method TryGetValueAttempts to retrieve the value that is associated with the specified key.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method IEnumerableGetEnumeratorReturns 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.