Udostępnij za pośrednictwem


DisplayExtensions.Display Method (HtmlHelper, String)

Returns HTML markup for each property in the object that is represented by a string expression.

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

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function Display ( _
    html As HtmlHelper, _
    expression As String _
) As MvcHtmlString
public static MvcHtmlString Display(
    this HtmlHelper html,
    string expression
)
[ExtensionAttribute]
public:
static MvcHtmlString^ Display(
    HtmlHelper^ html, 
    String^ expression
)

Parameters

  • expression
    Type: System.String
    An expression that identifies the object that contains the properties to display.

Return Value

Type: System.Web.Mvc.MvcHtmlString
The HTML markup for each property in the object that is represented by the expression.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type HtmlHelper. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

Use this method to display data from the ViewData dictionary or from a model. This method is useful if you do not know the type of the model. For more information about the differences between this method and the other Display methods, see the DisplayExtensions class overview.

This method generates different HTML markup depending on the data type of the property that is being rendered, and according to whether the property is marked with certain attributes. The method renders markup according to the following rules:

  • If the property is typed as a primitive type (integer, string, and so on), the method renders a string that represents the property value.

  • If the property type is Boolean, the method renders an HTML input element for a check box. For example, a Boolean property named Enabled might render markup such as the following:

    <input class="check-box"

    disabled="disabled"

    type="checkbox"

    checked="checked" />

  • If a property is annotated with a data type attribute, the attribute specifies the markup that is generated for the property. For example, if the property is marked with the EmailAddress attribute, the method generates markup that contains an HTML anchor that is configured with the mailto protocol, as in the following example:

    <a href='mailto:joe@contoso.com'>joe@contoso.com</a>

  • If the object contains multiple properties, for each property the method generates a string that consists of markup for the property name and for the property value.

Examples

A Visual Studio project with source code is available to accompany this topic: Download.

See Also

Reference

DisplayExtensions Class

Display Overload

System.Web.Mvc.Html Namespace

Other Resources

Walkthrough: Using Templated Helpers to Display Data in ASP.NET MVC