Partager via


ViewDataDictionary<TModel> Class

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

Inheritance Hierarchy

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

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Syntax

'Declaration
Public Class ViewDataDictionary(Of TModel) _
    Inherits ViewDataDictionary
public class ViewDataDictionary<TModel> : ViewDataDictionary
generic<typename TModel>
public ref class ViewDataDictionary : public ViewDataDictionary

Type Parameters

  • TModel
    The type of the model.

The ViewDataDictionary<TModel> type exposes the following members.

Constructors

  Name Description
Public method ViewDataDictionary<TModel>() Initializes a new instance of the ViewDataDictionary<TModel> class.
Public method ViewDataDictionary<TModel>(TModel) Initializes a new instance of the ViewDataDictionary<TModel> class by using the specified model.
Public method ViewDataDictionary<TModel>(ViewDataDictionary) Initializes a new instance of the ViewDataDictionary<TModel> class by using the specified view data dictionary.

Top

Properties

  Name Description
Public property Count Gets the number of elements in the collection. (Inherited from ViewDataDictionary.)
Public property IsReadOnly Gets a value that indicates whether the collection is read-only. (Inherited from ViewDataDictionary.)
Public property Item Gets or sets the item that is associated with the specified key. (Inherited from ViewDataDictionary.)
Public property Keys Gets a collection that contains the keys of this dictionary. (Inherited from ViewDataDictionary.)
Public property Model Gets or sets the model.
Public property ModelMetadata Gets or sets information about the model. (Overrides ViewDataDictionary.ModelMetadata.)
Public property ModelState Gets the state of the model. (Inherited from ViewDataDictionary.)
Public property TemplateInfo Gets or sets an object that encapsulates information about the current template context. (Inherited from ViewDataDictionary.)
Public property Values Gets a collection that contains the values in this dictionary. (Inherited from ViewDataDictionary.)

Top

Methods

  Name Description
Public method Add(KeyValuePair<String, Object>) Adds the specified item to the collection. (Inherited from ViewDataDictionary.)
Public method Add(String, Object) Adds an element to the collection using the specified key and value . (Inherited from ViewDataDictionary.)
Public method Clear Removes all items from the collection. (Inherited from ViewDataDictionary.)
Public method Contains Determines whether the collection contains the specified item. (Inherited from ViewDataDictionary.)
Public method ContainsKey Determines whether the collection contains an element that has the specified key. (Inherited from ViewDataDictionary.)
Public method CopyTo Copies the elements of the collection to an array, starting at a particular index. (Inherited from ViewDataDictionary.)
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Eval(String) Evaluates the specified expression. (Inherited from ViewDataDictionary.)
Public method Eval(String, String) Evaluates the specified expression by using the specified format. (Inherited from ViewDataDictionary.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Returns an enumerator that can be used to iterate through the collection. (Inherited from ViewDataDictionary.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetViewDataInfo Returns information about the view data as defined by the expression parameter. (Inherited from ViewDataDictionary.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Remove(KeyValuePair<String, Object>) Removes the first occurrence of a specified object from the collection. (Inherited from ViewDataDictionary.)
Public method Remove(String) Removes the element from the collection using the specified key. (Inherited from ViewDataDictionary.)
Protected method SetModel Sets the data model to use for the view. (Overrides ViewDataDictionary.SetModel(Object).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryGetValue Attempts to retrieve the value that is associated with the specified key. (Inherited from ViewDataDictionary.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method IEnumerable.GetEnumerator Returns an enumerator that can be used to iterate through the collection. (Inherited from ViewDataDictionary.)

Top

Remarks

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

Examples

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

Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations

Public Class Person
    <Required(ErrorMessage:="The ID is required.")> _
    Private _Id As Integer
    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property

    <Required(ErrorMessage:="The name is required.")> _
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    <Range(1, 200, ErrorMessage:="A number between 1 and 200.")> _
    Private _Age As Integer
    Public Property Age() As Integer
        Get
            Return _Age
        End Get
        Set(ByVal value As Integer)
            _Age = value
        End Set
    End Property

    <RegularExpression("((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", _
            ErrorMessage:="Invalid phone number.")> _
    Private _Phone As String
    Public Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
        End Set
    End Property

    <RegularExpression("^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", _
            ErrorMessage:="Invalid email address.")> _
    Private _Email As String
    Public Property Email() As String
        Get
            Return _Email
        End Get
        Set(ByVal value As String)
            _Email = value
        End Set
    End Property
End Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcDataViews.Models
{
    public class Person
    {
        [Required(ErrorMessage = "The ID is required.")]
        public int Id { get; set; }

        [Required(ErrorMessage = "The name is required.")]
        public string Name { get; set; }

        [Range(1, 200, ErrorMessage = "A number between 1 and 200.")]
        public int Age { get; set; }

        [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", 
            ErrorMessage = "Invalid phone number.")]
        public string Phone { get; set; }

        [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", 
            ErrorMessage = "Invalid email address.")]
        public string Email { 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.

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.

See Also

Reference

System.Web.Mvc Namespace