Search code examples
c#asp.net-mvchtml-helper

Difference between @Html.Display() and @Html.DisplayText()


What is the exact difference between @Html.Display() and @Html.DisplayText()? Both are giving the same output. I have a model class Employee which has a property

Name = "my Name".

And I am using Html helper in following way -

@Html.Display("Name")
@Html.DisplayText("Name")

The output for both methods is "my Name"


Solution

  • You can inspect the source code for Display and DisplayText.

    @Html.DisplayText(propertyName) will only output the the value of the property (defined by the SimpleDisplayText of ModelMetadata).

    @Html.Display(propertyName) provides far more options. If you have provided a DisplayTemplate from the property type, it will use the html in that template by default. You can also specify a specific template name to be used to generate the html. In additional, you can use the additionalViewData parameter of Display() to pass additional information to the template.

    In your case, you property is string and you have not defined a DisplayTemplate for string, therefore Display() uses the default (in-built) template which generates the same output as DisplayText()