Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between LabelFor and DisplayFor

Tags:

asp.net-mvc

I want to know exactly when to use @Html.DisplayFor and when to use @Html.LabelFor in MVC.

Everytime I work on them, it confuses me.

like image 219
nikhil kandalkar Avatar asked Dec 10 '14 12:12

nikhil kandalkar


People also ask

What is DisplayFor?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.

What is the difference between DisplayNameFor and DisplayFor in MVC?

DisplayFor displays the value for the model item and DisplayNameFor simply displays the name of the property?

What is the difference between label and LabelFor in MVC?

Answers. Label was introduced MVC 1 used in view templates. LabelFor was introduced in MVC2 is for "support strongly-typed HTML helpers that use lambda expressions when referencing models/viewmodels passed to a view template.

What is HTML LabelFor?

The <label> HTML element represents a caption for an item in a user interface.


1 Answers

suppose you have a property model like this:

[Display("Name:")]
public string Name{get;set;}

and you need to show in your view like this:

Name: Mohammad

then you can do this:

@HTML.LabelFor(m=>m.Name)// this will show "Name:"
@Html.DisplayFor(m=m>m.Name)// this will show (read only) "Mohammad"
like image 65
Mohammad Taradeh Avatar answered Sep 27 '22 18:09

Mohammad Taradeh