Html.Label,Html.LabelFor和Html.LabelForModel有什么区别 [英] What's the difference between Html.Label, Html.LabelFor and Html.LabelForModel
问题描述
@Html.Label()
,@Html.LabelFor()
和@Html.LabelForModel()
方法之间有什么区别?
What's the difference between @Html.Label()
, @Html.LabelFor()
and @Html.LabelForModel()
methods?
推荐答案
Html.Label
为您提供名称与指定输入文本匹配的输入标签(更具体地说,与字符串表达式匹配的model属性):>
Html.Label
gives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression):
// Model
public string Test { get; set; }
// View
@Html.Label("Test")
// Output
<label for="Test">Test</label>
Html.LabelFor
为您提供的表达式表示的属性的标签(通常是模型属性):
Html.LabelFor
gives you a label for the property represented by the provided expression (typically a model property):
// 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.LabelForModel
有点棘手.它返回一个标签,其标签的for
值是模型对象表示的参数的值.这对于自定义编辑器模板尤其有用.例如:
Html.LabelForModel
is a bit trickier. It returns a label whose for
value is that of the parameter represented by the model object. This is useful, in particular, for custom editor templates. For example:
// 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>
这篇关于Html.Label,Html.LabelFor和Html.LabelForModel有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!