Table of Contents

Class ModelMetadata

Namespace
System.Web.Mvc
Assembly
System.Web.Mvc.dll

Provides a container for common metadata, for the ModelMetadataProvider class, and for the ModelValidator class for a data model.

public class ModelMetadata
Inheritance
ModelMetadata
Derived
Inherited Members

Constructors

ModelMetadata(ModelMetadataProvider, Type, Func<object>, Type, string)

Initializes a new instance of the ModelMetadata class.

public ModelMetadata(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)

Parameters

provider ModelMetadataProvider

The provider.

containerType Type

The type of the container.

modelAccessor Func<object>

The model accessor.

modelType Type

The type of the model.

propertyName string

The name of the model.

Fields

DefaultOrder

The default order value, which is 10000.

public const int DefaultOrder = 10000

Field Value

int

Properties

AdditionalValues

Gets a dictionary that contains additional metadata about the model.

public virtual Dictionary<string, object> AdditionalValues { get; }

Property Value

Dictionary<string, object>

A dictionary that contains additional metadata about the model.

Container

A reference to the model's container object. Will be non-null if the model represents a property.

public object Container { get; set; }

Property Value

object

ContainerType

Gets or sets the type of the container for the model.

public Type ContainerType { get; }

Property Value

Type

The type of the container for the model.

ConvertEmptyStringToNull

Gets or sets a value that indicates whether empty strings that are posted back in forms should be converted to null.

public virtual bool ConvertEmptyStringToNull { get; set; }

Property Value

bool

true if empty strings that are posted back in forms should be converted to null; otherwise, false. The default value is true.

DataTypeName

Gets or sets meta information about the data type.

public virtual string DataTypeName { get; set; }

Property Value

string

Meta information about the data type.

Description

Gets or sets the description of the model.

public virtual string Description { get; set; }

Property Value

string

The description of the model. The default value is null.

DisplayFormatString

Gets or sets the display format string for the model.

public virtual string DisplayFormatString { get; set; }

Property Value

string

The display format string for the model.

DisplayName

Gets or sets the display name of the model.

public virtual string DisplayName { get; set; }

Property Value

string

The display name of the model.

EditFormatString

Gets or sets the edit format string of the model.

public virtual string EditFormatString { get; set; }

Property Value

string

The edit format string of the model.

HideSurroundingHtml

Gets or sets a value that indicates whether the model object should be rendered using associated HTML elements.

public virtual bool HideSurroundingHtml { get; set; }

Property Value

bool

true if the associated HTML elements that contains the model object should be included with the object; otherwise, false.

HtmlEncode

public virtual bool HtmlEncode { get; set; }

Property Value

bool

IsComplexType

Gets or sets a value that indicates whether the model is a complex type.

public virtual bool IsComplexType { get; }

Property Value

bool

A value that indicates whether the model is considered a complex type by the MVC framework.

IsNullableValueType

Gets a value that indicates whether the type is nullable.

public bool IsNullableValueType { get; }

Property Value

bool

true if the type is nullable; otherwise, false.

IsReadOnly

Gets or sets a value that indicates whether the model is read-only.

public virtual bool IsReadOnly { get; set; }

Property Value

bool

true if the model is read-only; otherwise, false.

IsRequired

Gets or sets a value that indicates whether the model is required.

public virtual bool IsRequired { get; set; }

Property Value

bool

true if the model is required; otherwise, false.

Model

Gets the value of the model.

public object Model { get; set; }

Property Value

object

The value of the model. For more information about ModelMetadata, see the entry ASP.NET MVC 2 Templates, Part 2: ModelMetadata on Brad Wilson's blog

ModelType

Gets the type of the model.

public Type ModelType { get; }

Property Value

Type

The type of the model.

NullDisplayText

Gets or sets the string to display for null values.

public virtual string NullDisplayText { get; set; }

Property Value

string

The string to display for null values.

Order

Gets or sets a value that represents order of the current metadata.

public virtual int Order { get; set; }

Property Value

int

The order value of the current metadata.

Properties

Gets a collection of model metadata objects that describe the properties of the model.

public virtual IEnumerable<ModelMetadata> Properties { get; }

Property Value

IEnumerable<ModelMetadata>

A collection of model metadata objects that describe the properties of the model.

PropertyName

Gets the property name.

public string PropertyName { get; }

Property Value

string

The property name.

Provider

Gets or sets the provider.

protected ModelMetadataProvider Provider { get; set; }

Property Value

ModelMetadataProvider

The provider.

RequestValidationEnabled

Gets or sets a value that indicates whether request validation is enabled.

public virtual bool RequestValidationEnabled { get; set; }

Property Value

bool

true if request validation is enabled; otherwise, false.

ShortDisplayName

Gets or sets a short display name.

public virtual string ShortDisplayName { get; set; }

Property Value

string

The short display name.

ShowForDisplay

Gets or sets a value that indicates whether the property should be displayed in read-only views such as list and detail views.

public virtual bool ShowForDisplay { get; set; }

Property Value

bool

true if the model should be displayed in read-only views; otherwise, false.

ShowForEdit

Gets or sets a value that indicates whether the model should be displayed in editable views.

public virtual bool ShowForEdit { get; set; }

Property Value

bool

true if the model should be displayed in editable views; otherwise, false.

SimpleDisplayText

Gets or sets the simple display string for the model.

public virtual string SimpleDisplayText { get; set; }

Property Value

string

The simple display string for the model.

TemplateHint

Gets or sets a hint that suggests what template to use for this model.

public virtual string TemplateHint { get; set; }

Property Value

string

A hint that suggests what template to use for this model.

Watermark

Gets or sets a value that can be used as a watermark.

public virtual string Watermark { get; set; }

Property Value

string

The watermark.

Methods

FromLambdaExpression<TParameter, TValue>(Expression<Func<TParameter, TValue>>, ViewDataDictionary<TParameter>)

Returns the metadata from the Expression parameter for the model.

public static ModelMetadata FromLambdaExpression<TParameter, TValue>(Expression<Func<TParameter, TValue>> expression, ViewDataDictionary<TParameter> viewData)

Parameters

expression Expression<Func<TParameter, TValue>>

An expression that identifies the model.

viewData ViewDataDictionary<TParameter>

The view data dictionary.

Returns

ModelMetadata

The metadata.

Type Parameters

TParameter

The type of the parameter.

TValue

The type of the value.

FromStringExpression(string, ViewDataDictionary)

Gets the metadata from the expression parameter for the model.

public static ModelMetadata FromStringExpression(string expression, ViewDataDictionary viewData)

Parameters

expression string

An expression that identifies the model.

viewData ViewDataDictionary

The view data dictionary.

Returns

ModelMetadata

The metadata for the model.

GetDisplayName()

Gets the display name for the model.

public string GetDisplayName()

Returns

string

The display name for the model.

GetSimpleDisplayText()

Returns the simple description of the model.

protected virtual string GetSimpleDisplayText()

Returns

string

The simple description of the model.

GetValidators(ControllerContext)

Gets a list of validators for the model.

public virtual IEnumerable<ModelValidator> GetValidators(ControllerContext context)

Parameters

context ControllerContext

The controller context.

Returns

IEnumerable<ModelValidator>

A list of validators for the model.