使用 CreateMetadata 自定义 ModelMetadata
当用户第一次从选择列表中选择一个值时,我们会显示一个默认选项,其中包含文本 Pick one
和值 -1
。
为了帮助解决这个问题,我们有一个自定义属性 NotEqualAttribute
。当需要下拉列表时,我们必须使用这两个属性才能使其正常工作。
我们的虚拟机看起来像这样:
[DisplayName("Pick Role")]
[UIHint("DropDownList")]
[Required]
[NotEqual("-1", "Select value for DropDown")]
public Int64 DdlSelected { get; set; }
public List<DDLDispValueCV> DdlSelectedList { get; set; }
我们更愿意使用这个:
[DisplayName("Pick Role")]
[UIHint("DropDownList")]
[Required]
public Int64 DdlSelected { get; set; }
public List<DDLDispValueCV> DdlSelectedList { get; set; }
另一个 StackOverflow 问题讨论了如何 使不可为 null 的属性可选,除非显式设置了 Required
属性。
我尝试这样做,但很困惑如何将新属性添加到 IEnumerable
集合中和/或将其添加到元数据提供程序中的 ModelMetadata
中:
public class MyMetaDataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
// Change how required is marked.
// If required attribute found mark the IsRequired true, otherwise false.
metadata.IsRequired = attributes.Where(x => x is RequiredAttribute).Count() > 0;
// If working with a drop down.
// And required attribute is present
// Then add NotEqual attribute
if ((metadata.TemplateHint != null) && (metadata.TemplateHint.ToString() == "DropDownList"))
{
// only add attribue if required.
if (metadata.IsRequired)
{
// Add custom attribute NotEqual.
NotEqualAttribute newAttr = new NotEqualAttribute("-1", "(Pick One) is not valid selection");
IEnumerable<Attribute> newAttrs = attributes.Concat(new[] {newAttr});
metadata = base.CreateMetadata(newAttrs, containerType, modelAccessor, modelType, propertyName);
metadata.IsRequired = true;
}
}
}
}
The first time a user chooses a value from a select list, we show a default option with the text Pick one
and value -1
.
To help with this we have a custom attribute NotEqualAttribute
. When a drop-down list is required we have to use both attributes to get it to work correctly.
Our VM looks something like:
[DisplayName("Pick Role")]
[UIHint("DropDownList")]
[Required]
[NotEqual("-1", "Select value for DropDown")]
public Int64 DdlSelected { get; set; }
public List<DDLDispValueCV> DdlSelectedList { get; set; }
We would prefer to use this:
[DisplayName("Pick Role")]
[UIHint("DropDownList")]
[Required]
public Int64 DdlSelected { get; set; }
public List<DDLDispValueCV> DdlSelectedList { get; set; }
Another StackOverflow question discusses how to make non-nullable properties optional unless the Required
property is explicitly set.
I tried to do this, but am confused how to add the new attribute to the IEnumerable<Attribute>
collection and/or add it to ModelMetadata
in the metadata provider:
public class MyMetaDataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
// Change how required is marked.
// If required attribute found mark the IsRequired true, otherwise false.
metadata.IsRequired = attributes.Where(x => x is RequiredAttribute).Count() > 0;
// If working with a drop down.
// And required attribute is present
// Then add NotEqual attribute
if ((metadata.TemplateHint != null) && (metadata.TemplateHint.ToString() == "DropDownList"))
{
// only add attribue if required.
if (metadata.IsRequired)
{
// Add custom attribute NotEqual.
NotEqualAttribute newAttr = new NotEqualAttribute("-1", "(Pick One) is not valid selection");
IEnumerable<Attribute> newAttrs = attributes.Concat(new[] {newAttr});
metadata = base.CreateMetadata(newAttrs, containerType, modelAccessor, modelType, propertyName);
metadata.IsRequired = true;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论