共用方式為


DisplayExtensions Class

Represents support for rendering object values as HTML.

Inheritance Hierarchy

System.Object
  System.Web.Mvc.Html.DisplayExtensions

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

Syntax

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class DisplayExtensions
public static class DisplayExtensions
[ExtensionAttribute]
public ref class DisplayExtensions abstract sealed

Methods

  Name Description
Public methodStatic member Display(HtmlHelper, String) Returns HTML markup for each property in the object that is represented by a string expression.
Public methodStatic member Display(HtmlHelper, String, Object) Returns HTML markup for each property in the object that is represented by a string expression, using additional view data.
Public methodStatic member Display(HtmlHelper, String, String) Returns HTML markup for each property in the object that is represented by the expression, using the specified template.
Public methodStatic member Display(HtmlHelper, String, String, Object) Returns HTML markup for each property in the object that is represented by the expression, using the specified template and additional view data.
Public methodStatic member Display(HtmlHelper, String, String, String) Returns HTML markup for each property in the object that is represented by the expression, using the specified template and an HTML field ID.
Public methodStatic member Display(HtmlHelper, String, String, String, Object) Returns HTML markup for each property in the object that is represented by the expression, using the specified template, HTML field ID, and additional view data.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>) Returns HTML markup for each property in the object that is represented by the Expression expression.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, Object) Returns a string that contains each property value in the object that is represented by the specified expression, using additional view data.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, String) Returns a string that contains each property value in the object that is represented by the Expression, using the specified template.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, String, Object) Returns a string that contains each property value in the object that is represented by the specified expression, using the specified template and additional view data.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, String, String) Returns HTML markup for each property in the object that is represented by the Expression, using the specified template and an HTML field ID.
Public methodStatic member DisplayFor<TModel, TValue>(HtmlHelper<TModel>, Expression<Func<TModel, TValue>>, String, String, Object) Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.
Public methodStatic member DisplayForModel(HtmlHelper) Returns HTML markup for each property in the model.
Public methodStatic member DisplayForModel(HtmlHelper, Object) Returns HTML markup for each property in the model, using additional view data.
Public methodStatic member DisplayForModel(HtmlHelper, String) Returns HTML markup for each property in the model using the specified template.
Public methodStatic member DisplayForModel(HtmlHelper, String, Object) Returns HTML markup for each property in the model, using the specified template and additional view data.
Public methodStatic member DisplayForModel(HtmlHelper, String, String) Returns HTML markup for each property in the model using the specified template and HTML field ID.
Public methodStatic member DisplayForModel(HtmlHelper, String, String, Object) Returns HTML markup for each property in the model, using the specified template, an HTML field ID, and additional view data.

Top

Remarks

The DisplayExtensions class provides the following methods that let you render values as HTML:

  • The Display method, which takes a string that represents the object value to render.

  • The DisplayFor method, which takes a model object.

  • The DisplayForModel method, which uses the model implicitly.

You can use the Display method to display data from the ViewData dictionary and from the object that is exposed by the Model property. If you do not know the type of the object that is exposed by the Model property, use the Display method.

The DisplayFor and DisplayForModel methods are typically used to display values from the object that is exposed by the Model property. You can also use these methods to display values from an object other than that exposed by the Model or ViewData properties.

Note

The two methods are often used in loop structures to render values from a collection of objects. The IndexPals.aspx page from the downloadable sample shows how to use the DisplayFor method in a loop.

The following example shows how to display a value that is not in the object that is exposed by the Model property.

<% var cv = Html.ViewContext.ClientValidationEnabled;  %> 
<%= Html.DisplayFor(model => cv) %>
<% Dim cv = Html.ViewContext.ClientValidationEnabled  %>
<%=Html.DisplayFor(Function(c) cv)%>

The model expressions that are passed to the methods are helpers that operate on the current model. The DisplayForModel call is functionally equivalent to calling the DisplayFor method and passing a model as a parameter. The following examples shows how to use each of these methods. The value that is rendered for each method is the same.

<%= Html.DisplayForModel() %>
<%= Html.DisplayFor(model => cv) %>
<%=Html.DisplayForModel()%>
<%=Html.DisplayFor(Function(c) cv)%>

The DisplayExtensions class uses the ViewEngineCollection instance to resolve which view to pass the model into. Therefore, you can write display templates for any view engine. For more information on alternative view engines see the following links:

Examples

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

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.Html Namespace

Other Resources

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