htmlhelper display displayfor ASP.NET MVC
In ASP.NET MVC, the Display
HTML helper method and its strongly-typed counterpart DisplayFor
are used to display the value of a property of the model in a view without allowing the user to modify the value.
Display
HTML helper:
TheDisplay
HTML helper is a general-purpose helper method that displays the value of a property of the model in the view. Here is an example usage ofDisplay
:
@Html.Display("Name", Model.Name)
This will generate plain text that displays the value of Model.Name
property in the view.
DisplayFor
HTML helper:
TheDisplayFor
HTML helper is a strongly-typed helper method that displays the value of a property of the model that is passed to the view. Here is an example usage ofDisplayFor
:
@Html.DisplayFor(model => model.Name)
This will generate plain text that displays the value of Model.Name
property in the view. The DisplayFor
helper method uses lambda expressions to generate the name and initial value of the input element based on the model property that is passed to it.
Using DisplayFor
is generally considered to be better practice than using Display
because it provides stronger type checking and reduces the likelihood of runtime errors. Additionally, it helps to prevent against cross-site scripting (XSS) attacks by automatically encoding the input.