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)

Inheritance Hierarchy
SystemObject
  System.Web.MvcViewDataDictionary
     System.Web.MvcViewDataDictionary

Syntax
public class ViewDataDictionary : IDictionary<stringobject>, 
	ICollection<KeyValuePair<stringobject>>, IEnumerable<KeyValuePair<stringobject>>, 
	IEnumerable
public ref class ViewDataDictionary : IDictionary<String^, Object^>, 
	ICollection<KeyValuePair<String^, Object^>>, IEnumerable<KeyValuePair<String^, Object^>>, 
	IEnumerable
type ViewDataDictionary = 
    class
        interface IDictionary<string, Object>
        interface ICollection<KeyValuePair<string, Object>>
        interface IEnumerable<KeyValuePair<string, Object>>
        interface IEnumerable
    end
Public Class ViewDataDictionary
	Implements IDictionary(Of StringObject), ICollection(Of KeyValuePair(Of StringObject)),
	IEnumerable(Of KeyValuePair(Of StringObject)), IEnumerable
Constructors
NameDescription
System_CAPS_pubmethod ViewDataDictionary

Initializes a new instance of the ViewDataDictionary class.

System_CAPS_pubmethod ViewDataDictionary

Initializes a new instance of the ViewDataDictionary class by using the specified model.

System_CAPS_pubmethod ViewDataDictionary

Initializes a new instance of the ViewDataDictionary class by using the specified dictionary.

Properties
NameDescription
System_CAPS_pubproperty

Gets the number of elements in the collection.

System_CAPS_pubproperty

Gets a value that indicates whether the collection is read-only.

System_CAPS_pubproperty

Gets or sets the item that is associated with the specified key.

System_CAPS_pubproperty

Gets a collection that contains the keys of this dictionary.

System_CAPS_pubproperty

Gets or sets the model that is associated with the view data.

System_CAPS_pubproperty

Gets or sets information about the model.

System_CAPS_pubproperty

Gets the state of the model.

System_CAPS_pubproperty

Gets or sets an object that encapsulates information about the current template context.

System_CAPS_pubproperty

Gets a collection that contains the values in this dictionary.

Methods
NameDescription
System_CAPS_pubmethod Add

Adds the specified item to the collection.

System_CAPS_pubmethod Add

Adds an element to the collection using the specified key and value .

System_CAPS_pubmethod Clear

Removes all items from the collection.

System_CAPS_pubmethod Contains

Determines whether the collection contains the specified item.

System_CAPS_pubmethod ContainsKey

Determines whether the collection contains an element that has the specified key.

System_CAPS_pubmethod CopyTo

Copies the elements of the collection to an array, starting at a particular index.

System_CAPS_pubmethod Equals

(Inherited from Object.)

System_CAPS_pubmethod Eval

Evaluates the specified expression.

System_CAPS_pubmethod Eval

Evaluates the specified expression by using the specified format.

System_CAPS_protmethod Finalize

(Inherited from Object.)

System_CAPS_pubmethod GetEnumerator

Returns an enumerator that can be used to iterate through the collection.

System_CAPS_pubmethod GetHashCode

(Inherited from Object.)

System_CAPS_pubmethod GetType

(Inherited from Object.)

System_CAPS_pubmethod GetViewDataInfo

Returns information about the view data as defined by the expression parameter.

System_CAPS_protmethod MemberwiseClone

(Inherited from Object.)

System_CAPS_pubmethod Remove

Removes the first occurrence of a specified object from the collection.

System_CAPS_pubmethod Remove

Removes the element from the collection using the specified key.

System_CAPS_protmethod SetModel

Sets the data model to use for the view.

System_CAPS_pubmethod ToString

(Inherited from Object.)

System_CAPS_pubmethod TryGetValue

Attempts to retrieve the value that is associated with the specified key.

Explicit Interface Implementations
NameDescription
System_CAPS_pubinterface System_CAPS_privmethod IEnumerableGetEnumerator

Returns an enumerator that can be used to iterate through the collection.

Remarks

The 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 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 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 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.

Return to top