asp.net-mvc Html.Label、Html.LabelFor 和 Html.LabelForModel 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16346179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between Html.Label, Html.LabelFor and Html.LabelForModel
提问by Fabricio
What's the difference between @Html.Label(), @Html.LabelFor()and @Html.LabelForModel()methods?
什么之间的区别@Html.Label(),@Html.LabelFor()和@Html.LabelForModel()方法呢?
回答by Ant P
Html.Labelgives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression):
Html.Label为您提供名称与指定输入文本匹配的输入的标签(更具体地说,对于与字符串表达式匹配的模型属性):
// Model
public string Test { get; set; }
// View
@Html.Label("Test")
// Output
<label for="Test">Test</label>
Html.LabelForgives you a label for the property represented by the provided expression (typically a model property):
Html.LabelFor为您提供由提供的表达式(通常是模型属性)表示的属性的标签:
// Model
public class MyModel
{
[DisplayName("A property")]
public string Test { get; set; }
}
// View
@model MyModel
@Html.LabelFor(m => m.Test)
// Output
<label for="Test">A property</label>
Html.LabelForModelis a bit trickier. It returns a label whose forvalue is that of the parameter represented by the model object. This is useful, in particular, for custom editor templates. For example:
Html.LabelForModel有点棘手。它返回一个标签,其for值是模型对象表示的参数的值。这对于自定义编辑器模板尤其有用。例如:
// Model
public class MyModel
{
[DisplayName("A property")]
public string Test { get; set; }
}
// Main view
@Html.EditorFor(m => m.Test)
// Inside editor template
@Html.LabelForModel()
// Output
<label for="Test">A property</label>
回答by CorrugatedAir
Html.Label- Just creates a label tag with whatever the string passed into the constructor is
Html.Label- 只需使用传递给构造函数的字符串创建一个标签标签
Html.LabelFor- Creates a label for that specific property. This is strongly typed. By default, this will just do the name of the property (in the below example, it'll output MyProperty if that Display attribute wasn't there). Another benefit of this is you can set the display property in your model and that's what will be put here:
Html.LabelFor- 为该特定属性创建标签。这是强类型的。默认情况下,这只会执行属性的名称(在下面的示例中,如果该 Display 属性不存在,它将输出 MyProperty)。这样做的另一个好处是您可以在模型中设置 display 属性,这就是将放在此处的内容:
public class MyModel
{
[Display(Name="My property title")
public class MyProperty{get;set;}
}
In your view:
在您看来:
Html.LabelFor(x => x.MyProperty) //Outputs My property title
In the above, LabelFor will display <label for="MyProperty">My property title</label>. This works nicely so you can define in one place what the label for that property will be and have it show everywhere.
在上面, LabelFor 将显示<label for="MyProperty">My property title</label>. 这很好用,因此您可以在一个地方定义该属性的标签并将其显示在任何地方。
回答by AlexMelw
I think that the usage of @Html.LabelForModel()should be explained in more detail.
我认为@Html.LabelForModel()应该更详细地解释的用法。
The LabelForModel Method returns an HTML label element and the property name of the property that is represented by the model.
LabelForModel 方法返回一个 HTML 标签元素和由模型表示的属性的属性名称。
You could refer to the following code:
您可以参考以下代码:
Code in model:
模型中的代码:
using System.ComponentModel;
[DisplayName("MyModel")]
public class MyModel
{
[DisplayName("A property")]
public string Test { get; set; }
}
Code in view:
代码视图:
@Html.LabelForModel()
<div class="form-group">
@Html.LabelFor(model => model.Test, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Test)
@Html.ValidationMessageFor(model => model.Test)
</div>
</div>
The output screenshot:
输出截图:
回答by Mahesh Chitroda
suppose you need a label with text customername than you can achive it using 2 ways
假设您需要一个带有文本 customername 的标签,而您可以使用 2 种方式实现它
[1]@Html.Label("CustomerName")
[2]@Html.LabelFor(a => a.CustomerName) //strongly typed
2nd method used a property from your model. If your view implements a model then you can use the 2nd method.
第二种方法使用了模型中的属性。如果您的视图实现了一个模型,那么您可以使用第二种方法。
More info please visit below link
更多信息请访问以下链接
http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx
http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx


