Html.Label 和 Html.Display 之间有什么区别?
Html.Label
和 Html.Display
之间有什么区别?
What is the difference between Html.Label
and Html.Display
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Html.Label()
呈现可用于模型实体属性的 HTML 标记。
例如,一方面
会呈现
Html.Display()
,另一方面会根据指定的模板呈现整个实体的 HTML 标记。例如。如果您有一个具有多个属性的 Person 实体,您可以定义一个模板,其中包含有关如何呈现 Person 的标记,并且Html.Display()
使用该模板跨视图呈现 Person 对象。 Phil Haack 有一篇关于显示模板的优秀帖子。Html.Label()
renders HTML markup<label />
that can be used for a model entity's attrubute.For eg,
would render
Html.Display()
on the other hand renders HTML markup for entire entity based on specified templates. For eg. if you have a Person entity with multiple attributes, you define a template with markup as to how to render a Person andHtml.Display()
uses that template to render Person objects across views. Phil Haack has an excellent post on display templates.Label
返回 HTML 标签元素并由指定表达式表示的属性的属性名称。
显示
返回每个属性的 HTML 标记在由字符串表达式表示的对象中。
Label
Returns an HTML label element and the property name of the property that is represented by the specified expression.
Display
Returns HTML markup for each property in the object that is represented by a string expression.
Html.Display 更加动态,它根据正在呈现的属性的数据类型以及该属性是否使用某些属性进行标记来生成不同的 HTML 标记。该方法根据以下规则呈现标记:
如果属性的类型为基本类型(整数、字符串等),则该方法呈现表示属性值的字符串。
如果属性使用数据类型属性进行注释,则该属性指定为该属性生成的标记。例如,如果该属性使用 EmailAddress 属性进行标记,则该方法会生成包含使用 mailto 协议配置的 HTML 锚点的标记,如下例所示:
如果该对象包含多个属性,则该方法会为每个属性生成一个由属性名称和属性值的标记组成的字符串。
Html.Label 只是生成一个类似 Male 的 label 标签
Html.Display is more dynamic it generates different HTML markup depending on the data type of the property that is being rendered, and according to whether the property is marked with certain attributes. The method renders markup according to the following rules:
If the property is typed as a primitive type (integer, string, and so on), the method renders a string that represents the property value.
If a property is annotated with a data type attribute, the attribute specifies the markup that is generated for the property. For example, if the property is marked with the EmailAddress attribute, the method generates markup that contains an HTML anchor that is configured with the mailto protocol, as in the following example:
If the object contains multiple properties, for each property the method generates a string that consists of markup for the property name and for the property value.
Html.Label just generates a label tag like Male