Export (0) Print
Expand All

ViewDataDictionary<TModel> Class

Represents a container that is used to pass strongly typed 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<TModel> : ViewDataDictionary

Type Parameters

TModel

The type of the model.

The ViewDataDictionary<TModel> type exposes the following members.

  NameDescription
Public methodViewDataDictionary<TModel>()Initializes a new instance of the ViewDataDictionary<TModel> class.
Public methodViewDataDictionary<TModel>(TModel)Initializes a new instance of the ViewDataDictionary<TModel> class by using the specified model.
Public methodViewDataDictionary<TModel>(ViewDataDictionary)Initializes a new instance of the ViewDataDictionary<TModel> class by using the specified view data dictionary.
Top

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

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

  NameDescription
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an enumerator that can be used to iterate through the collection. (Inherited from ViewDataDictionary.)
Top

You can use an instance of the strongly typed ViewDataDictionary<TModel> class in the same way that you use the loosely typed ViewDataDictionary class. An instance of the ViewDataDictionary<TModel> class is exposed by the ViewData property of the ViewPage<TModel> class.

To pass strongly typed data to a view, change the @ Page directive of the view so that the view inherits from ViewPage<TModel> instead of from ViewPage, as shown in the following example:

<%@ Page Inherits="ViewPage<Person>" %>

The following example shows the definition of a typical data model class named Person.


public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}


The following example shows a view that enables the user to modify the values of a Person object and to submit the changes for update. The value that is passed to each text box is a property of the model object that was passed by using the ViewDataDictionary<TModel> object. For example, Model.Id corresponds to the Person.Id property.


<h2>Edit</h2>

<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Id">Id:</label>
            <%= Html.TextBox("Id", Model.Id) %>
            <%= Html.ValidationMessage("Id", "*") %>
        </p>
        <p>
            <label for="Name">Name:</label>
            <%= Html.TextBox("Name", Model.Name) %>
            <%= Html.ValidationMessage("Name", "*") %>
        </p>
        <p>
            <label for="Age">Age:</label>
            <%= Html.TextBox("Age", Model.Age) %>
            <%= Html.ValidationMessage("Age", "*") %>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

<div>
    <%=Html.ActionLink("Back to List", "Index") %>
</div>


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