API Reference: MaxLengthAttribute Class
This attribute is but part of the .NET System.ComponentModel.DataAnnotations
namespace.
The MaxLengthAttribute
can be applied to string-based content model properties. The attribute sets the maximum allowed character length of a string property value. The back office will display a validation error if the property value length is more than the specified maximum. If the property type is a collection (f.e. IList<string>
), every value in the collection is validated individually.
You can combine this attribute with the MinLength
attribute to specify a min/max length range.
Usage
Decorate the model property with the MaxLengthAttribute
. Pass the maximum allowed length in the attribute constructor.
Example
The following example sets a maximum length of 50 characters for the Title property (as well as a minimum length of 10).
public class SampleContent: IContentType
{
[MinLength(10)]
[MaxLength(50)]
public string Title { get; set; }
// ...
}