且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以下代码的含义是什么?

更新时间:2022-06-20 04:06:49

,ErrorMessage = 请输入有效的电子邮件)]
[显示(名称= 电子邮件)]
public string ContactEmail { get ; set ;}
", ErrorMessage = "Please enter a valid email")] [Display(Name = "Email")] public string ContactEmail { get; set; }


这些应用于给定属性的4个属性,通常它们在验证的上下文中使用,这里的含义是:

1. 必需 - 根据需要标记属性,如果用户忘记输入此属性的值,将显示相关消息。

2. StringLength - 将字符串的最大长度设置为100,但在这种情况下未正确设置错误消息。请参阅此处的详细信息: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute(V = vs.110) .aspx [ ^ ]

3. RegularExpression 设置将用于验证用户输入。

4. 显示 - 设置此属性的默认显示名称。这将在生成的Views中的ASP.NET MVC中用作此属性的标签名称。
These are 4 attribuits applied to the given property, and normally they are used in the context of validation, and here are the meaning:
1. Required - mark the property as required, and if the user forgot to enter a value for this property the associated message will be shown.
2. StringLength - set the max length of the string to 100, but the error message is not set correctly in this case. See details here: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute(v=vs.110).aspx[^]
3. RegularExpression set the regular expression that will be used to validate the user input.
4. Display - set the default display name for this property. This will be used in ASP.NET MVC in the generated Views as the name of the label for this property.