StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First

前端 未结 8 1637
情歌与酒
情歌与酒 2020-12-04 07:47

What is the difference in behavior of [MaxLength] and [StringLength] attributes?

As far as I can tell (with the exception that [MaxLe

相关标签:
8条回答
  • 2020-12-04 08:15

    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.

    0 讨论(0)
  • 2020-12-04 08:15

    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.

    0 讨论(0)
提交回复
热议问题