Asp.net mvc LabelFor和DisplayFor之间的差异

Asp.net mvc LabelFor和DisplayFor之间的差异,asp.net-mvc,Asp.net Mvc,我想知道什么时候在MVC中使用@Html.DisplayFor以及什么时候使用@Html.LabelFor 每次我处理它们时,我都会感到困惑 假设您有这样一个属性模型: [Display("Name:")] public string Name{get;set;} 您需要在视图中显示如下内容: [Display("Name:")] public string Name{get;set;} 姓名:穆罕默德 然后你可以这样做: @Html.DisplayFor(m=m>m.Name)//

我想知道什么时候在MVC中使用
@Html.DisplayFor
以及什么时候使用
@Html.LabelFor


每次我处理它们时,我都会感到困惑

假设您有这样一个属性模型:

[Display("Name:")]
public string Name{get;set;}
您需要在视图中显示如下内容:

[Display("Name:")]
public string Name{get;set;}
姓名:穆罕默德

然后你可以这样做:

@Html.DisplayFor(m=m>m.Name)// this will show (read only) "Mohammad"
@HTML.LabelFor(m=>m.Name)// this will show "Name:"

此外,
LabelFor
如果与
复选框一起使用,则可以单击,
DisplayFor
将不显示。

LabelFor
显示属性名称,并与表单中的控件(输入、选择等)相关联
DisplayFor
呈现属性的值,因此,
DisplayFor
将尝试使用a,如果您已经定义了一个属性。@StephenMuecke您应该写一个答案,因为您完全正确您的答案是错误的(
LabelFor()
将输出“Name”,而
DisplayFor()
将输出“Mohammad”)