This topic has not yet been rated - Rate this topic

ViewDataDictionary Class

Represents a container that is used to pass data between a controller and a view.

System.Object
  System.Web.Mvc.ViewDataDictionary
    System.Web.Mvc.ViewDataDictionary<TModel>

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.

  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
  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
  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 (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 GetEnumerator Returns 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 GetViewDataInfo Returns information about the view data as defined by the expression parameter.
Protected method MemberwiseClone (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 (Inherited from Object.)
Public method TryGetValue Attempts to retrieve the value that is associated with the specified key.
Top
  Name Description
Explicit interface implemetation Private method IEnumerable.GetEnumerator Returns 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.
Did you find this helpful?
(1500 characters remaining)