StringLength 与 MaxLength 属性 ASP.NET MVC 与 Entity Framework EF Code First

发布于 2024-11-02 06:56:44 字数 140 浏览 1 评论 0原文

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

黎夕旧梦 2024-11-09 06:56:45

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:

Specifies the maximum length of array
or string data allowed in a property.

StringLength is a data annotation that will be used for validation of user input.

From MSDN:

Specifies the minimum and maximum
length of characters that are allowed
in a data field.

撩发小公举 2024-11-09 06:56:45

我刚刚从另一篇文章中了解到一些快速但非常有用的附加信息,但似乎无法找到相关文档(如果有人可以在 MSDN 上共享指向它的链接,那就太棒了):

与这些属性关联的验证消息将实际上替换与属性关联的占位符。例如:

[MaxLength(100, "{0} can have a max of {1} characters")]
public string Address { get; set; }

如果超过字符限制,将输出以下内容:
“地址最多可包含 100 个字符”

我知道的占位符是:

  • {0} = 属性名称
  • {1} = 最大长度
  • {2} = 最小长度

非常感谢 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:

[MaxLength(100, "{0} can have a max of {1} characters")]
public string Address { get; set; }

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:

  • {0} = Property Name
  • {1} = Max Length
  • {2} = Min Length

Much thanks to bloudraak for initially pointing this out.

别理我 2024-11-09 06:56:45

以下是我们在 EF code first 中同时使用 [MaxLength][StringLength] 属性时的结果。如果两者都使用,[MaxLength] 赢得比赛。请参阅下面班级的 studentname 列中的测试结果

 public class Student
 {
    public Student () {}

    [Key]
    [Column(Order=1)]
    public int StudentKey { get; set; }

    //[MaxLength(50),StringLength(60)]    //studentname column will be nvarchar(50)
    //[StringLength(60)]    //studentname column will be nvarchar(60)
    [MaxLength(50)] //studentname column will be nvarchar(50)
    public string StudentName { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }
 }

Following are the results when we use both [MaxLength] and [StringLength] attributes, in EF code first. If both are used, [MaxLength] wins the race. See the test result in studentname column in below class

 public class Student
 {
    public Student () {}

    [Key]
    [Column(Order=1)]
    public int StudentKey { get; set; }

    //[MaxLength(50),StringLength(60)]    //studentname column will be nvarchar(50)
    //[StringLength(60)]    //studentname column will be nvarchar(60)
    [MaxLength(50)] //studentname column will be nvarchar(50)
    public string StudentName { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }
 }
2024-11-09 06:56:45

所有好的答案......从验证的角度来看,我还注意到 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.

抱着落日 2024-11-09 06:56:45

另一点需要注意的是,在 MaxLength 属性中,您只能提供 ma​​x 所需范围,而不是 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.

孤星 2024-11-09 06:56:45

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/

拥抱影子 2024-11-09 06:56:45

您可以使用:

[StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)]
public string Address { get; set; }

前面的代码创建的错误消息将是“地址长度必须在 6 到 8 之间。”。

MSDN:https://learn。 microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0

You can use :

[StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)]
public string Address { get; set; }

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

生来就爱笑 2024-11-09 06:56:45

我通过在我的上下文中添加以下行解决了这个问题:

modelBuilder.Entity<YourObject>().Property(e => e.YourColumn).HasMaxLength(4000);

不知何故,[MaxLength] 对我不起作用。

I have resolved it by adding below line in my context:

modelBuilder.Entity<YourObject>().Property(e => e.YourColumn).HasMaxLength(4000);

Somehow, [MaxLength] didn't work for me.

总攻大人 2024-11-09 06:56:45

当使用该属性限制网页上表单文本的最大输入长度时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文