StringLengthAttribute
既存の ASP.NET MVC 4 プロジェクトの多くのモデルに検証を追加する必要があり、 DataAnnotationsModelMetadataProvider
.
および他のいくつかのデータ注釈属性と完全に連携しRequiredAttribute
ます (クライアント側とサーバー側の両方の検証が機能します) が、文字列の長さの検証は追加されません。以下の最小限の例を見つけてください。
public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType,
string propertyName)
{
StringLengthAttribute lengthAttribute = new StringLengthAttribute(256);
attributes = attributes.Union(new[] { lengthAttribute });
return base.CreateMetadata(attributes,
containerType,
modelAccessor,
modelType,
propertyName);
}
}
そのため、StringLengthAttribute
特別な方法で処理されているようです。それを機能させる方法やより良い実装のアイデアについてのアイデアはありますか?