StringLength 与 MaxLength 属性 ASP.NET MVC 与 Entity Framework EF Code First
[MaxLength]
和 [StringLength]
属性的行为有什么区别?
据我所知(除了 [MaxLength]
可以验证数组的最大长度)这些是相同的并且有些冗余?
What is the difference in behavior of [MaxLength]
and [StringLength]
attributes?
As far as I can tell (with the exception that [MaxLength]
can validate the maximum length of an array) these are identical and somewhat redundant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
MaxLength 用于实体框架在创建数据库时决定将字符串值字段设置为多大。
来自 MSDN:
StringLength 是用于验证用户输入的数据注释。
来自 MSDN:
MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database.
From MSDN:
StringLength is a data annotation that will be used for validation of user input.
From MSDN:
我刚刚从另一篇文章中了解到一些快速但非常有用的附加信息,但似乎无法找到相关文档(如果有人可以在 MSDN 上共享指向它的链接,那就太棒了):
与这些属性关联的验证消息将实际上替换与属性关联的占位符。例如:
如果超过字符限制,将输出以下内容:
“地址最多可包含 100 个字符”
我知道的占位符是:
非常感谢 bloudraak 最初指出了这一点。
Some quick but extremely useful additional information that I just learned from another post, but can't seem to find the documentation for (if anyone can share a link to it on MSDN that would be amazing):
The validation messages associated with these attributes will actually replace placeholders associated with the attributes. For example:
Will output the following if it is over the character limit:
"Address can have a max of 100 characters"
The placeholders I am aware of are:
Much thanks to bloudraak for initially pointing this out.
以下是我们在
EF code first
中同时使用[MaxLength]
和[StringLength]
属性时的结果。如果两者都使用,[MaxLength]
赢得比赛。请参阅下面班级的studentname
列中的测试结果Following are the results when we use both
[MaxLength]
and[StringLength]
attributes, inEF code first
. If both are used,[MaxLength]
wins the race. See the test result instudentname
column in below class所有好的答案......从验证的角度来看,我还注意到 MaxLength 仅在服务器端得到验证,而 StringLength 也在客户端得到验证。
All good answers...From the validation perspective, I also noticed that MaxLength gets validated at the server side only, while StringLength gets validated at client side too.
另一点需要注意的是,在 MaxLength 属性中,您只能提供 max 所需范围,而不是 min 所需范围。
在StringLength中,您可以提供两者。
One another point to note down is in MaxLength attribute you can only provide max required range not a min required range.
While in StringLength you can provide both.
MaxLengthAttribute 表示最大长度。允许的数组或字符串数据的长度
StringLengthAttribute 表示 Min。和最大。数据字段中允许的字符长度
访问 http://joeylicc.wordpress.com/2013/06/20/asp-net-mvc-model-validation-using-data-annotations/
MaxLengthAttribute means Max. length of array or string data allowed
StringLengthAttribute means Min. and max. length of characters that are allowed in a data field
Visit http://joeylicc.wordpress.com/2013/06/20/asp-net-mvc-model-validation-using-data-annotations/
您可以使用:
前面的代码创建的错误消息将是“地址长度必须在 6 到 8 之间。”。
MSDN:https://learn。 microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0
You can use :
The error message created by the preceding code would be "Address length must be between 6 and 8.".
MSDN: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0
我通过在我的上下文中添加以下行解决了这个问题:
不知何故,
[MaxLength]
对我不起作用。I have resolved it by adding below line in my context:
Somehow,
[MaxLength]
didn't work for me.当使用该属性限制网页上表单文本的最大输入长度时,StringLength 似乎会生成 maxlength html 属性(至少在我使用 MVC 5 进行的测试中)。选择哪一种取决于您希望如何提醒用户这是最大文本长度。使用 stringlength 属性,用户将无法输入超出允许长度的内容。 maxlength 属性不会添加此 html 属性,而是生成数据验证属性,这意味着用户可以键入超出指示的长度,并且防止更长的输入取决于当他移动到下一个字段或单击提交(或如果禁用 javascript,则进行服务器端验证)。在这种情况下,可以通过错误消息向用户通知限制。
When using the attribute to restrict the maximum input length for text from a form on a webpage, the StringLength seems to generate the maxlength html attribute (at least in my test with MVC 5). The one to choose then depnds on how you want to alert the user that this is the maximum text length. With the stringlength attribute, the user will simply not be able to type beyond the allowed length. The maxlength attribute doesn't add this html attribute, instead it generates data validation attributes, meaning the user can type beyond the indicated length and that preventing longer input depends on the validation in javascript when he moves to the next field or clicks submit (or if javascript is disabled, server side validation). In this case the user can be notified of the restriction by an error message.