htmlhelper display displayfor ASP.NET MVC

www.igif‮c.aedit‬om

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.

  1. Display HTML helper:
    The Display 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 of Display:
@Html.Display("Name", Model.Name)

This will generate plain text that displays the value of Model.Name property in the view.

  1. DisplayFor HTML helper:
    The DisplayFor 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 of DisplayFor:
@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.