简体   繁体   English

c#属性显示我们为什么使用它们?

[英]c# attribute Display why we use them?

this code 此代码

[Display(Name="Name")]
public string StudentName { get; set; }

why use the Display attribute and how i can use them ? 为什么使用Display属性以及如何使用它们? for what ! 为了什么 !

The Display attribute is used to modify the text that displays on the screen when a control is rendered for your view. Display属性用于修改为视图渲染控件时在屏幕上显示的文本。

For example (in your view CSHTML file): 例如(在您的视图CSHTML文件中):

@Html.LabelFor(x => x.StudentName)

Will display a label with the text "Name" because of the Display attribute in your example, rather than the default value of "Student Name" had you not included the attribute. 由于示例中的“显示”属性,将显示带有文本“名称”的标签,如果您未包括该属性,则将显示默认值“学生名称”。 This is also true of validation messages. 验证消息也是如此。

See "Remarks" here: 请参阅此处的“备注”: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.name%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.displayattribute.name%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM