使用 CreateMetadata 自定义 ModelMetadata

发布于 2024-11-06 16:29:29 字数 2528 浏览 0 评论 0原文

当用户第一次从选择列表中选择一个值时,我们会显示一个默认选项,其中包含文本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文