null 非許容型のプロパティには常に RequiredAttribue が必要であると想定される理由を誰でも説明できますか?
フィールドが必須であることをユーザーに示すことができるように、* を自動追加するか、css クラスを変更するラベル ヘルパーを作成しようとしています。ただし、メタデータのクエリを実行すると、null 不可のプロパティが必要な属性で終了します。
MVC ソース コード:
protected override IEnumerable<ModelValidator> GetValidators(
ModelMetadata metadata, ControllerContext context,
IEnumerable<Attribute> attributes)
{
_adaptersLock.EnterReadLock();
try
{
List<ModelValidator> results = new List<ModelValidator>();
if (metadata.IsRequired &&
!attributes.Any(a => a is RequiredAttribute))
{
//******* Why Do this?
attributes = attributes.Concat(new[] { new RequiredAttribute() });
}
foreach (ValidationAttribute attribute in
attributes.OfType<ValidationAttribute>())
{
DataAnnotationsModelValidationFactory factory;
if (!_adapters.TryGetValue(attribute.GetType(), out factory))
factory = _defaultFactory;
results.Add(factory(metadata, context, attribute));
}
return results;
}
finally
{
_adaptersLock.ExitReadLock();
}
}