最新Html.Label,Html.LabelFor和Html.LabelForModel之间的差 [英] Whats the difference between Html.Label, Html.LabelFor and Html.LabelForModel
问题描述
有什么 @ Html.Label()
, @ Html.LabelFor()
和<$ C之间的差异$ C> @ Html.LabelForModel()的方法呢?
推荐答案
Html.Label
给你输入其名称与指定的输入文本相匹配的标签(更具体为匹配字符串前pression模型属性):
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
为您提供由提供的前pression(通常是模型属性)psented属性重新$ P $标签:
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
是有点棘手。它返回一个标签的为
值是由模型对象psented参数重新$ P $的。这是有用的,特别是对自定义编辑器模板。例如:
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屋!