在 MVC 3.0 中,当将 IsRequired 设置为属性的 ModelMetadata 时,它不会添加验证

发布于 2025-01-04 00:28:46 字数 2092 浏览 0 评论 0原文

我的应用程序中有某些属性需要动态设置,无论它们是否是必需的,因此我无法使用数据注释的 [Required] 属性。

也许这不是实现我想要的最好的方式。所以我很乐意接受这方面的任何建议。 我已经用以下方法覆盖了 DataAnnotationsModelMetadataProvider:

public class DynamicFieldsMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        {
            if (containerType == null)
                throw new ArgumentNullException("containerType");

            if (!typeof(DynamicFieldDataItem).IsAssignableFrom(containerType))
                foreach (var metadataProperty in base.GetMetadataForProperties(container, containerType))
                    yield return metadataProperty;
            else 
                foreach (var metadataProperty in base.GetMetadataForProperties(container, containerType))
                {
                    var dynamicField = (DynamicFieldDataItem)container;
                    if (metadataProperty.PropertyName == "DataFieldValue")
                        metadataProperty.IsRequired = dynamicField.IsRequired;
                    yield return metadataProperty;
                }

        }
}

这只是一个概念测试,一旦我使其工作,我会将其更改为动态且更面向对象的东西,但到目前为止,只能将属性 DataFieldValue 的 MetadataModel 设置为 IsRequired = true 我可以开始了。

有了这个,我成功地以动态方式将 IsRequired 属性设置为 true (我想,有了这个就足够了!),当我在我的视图中调试时:

@Html.EditorFor(model=>model.DataFieldValue)

属性 DataFieldValue 声明如下:

    public class DynamicFieldDataItem
    {
        public string DataFieldValue { get; set; }
        public bool IsRequired{ get; set; }
    }

我可以看到元数据,具有属性 IsRequired 为 true,但是当呈现“DataFieldValue”时,“验证器”不存在,当然验证不起作用。

为了确保我的项目配置没有问题,我检查了 web.config 并包含用于验证的 javascript,所有配置均正确。更重要的是,如果我将“Required”属性添加到我的属性中,如下所示:

public class DynamicFieldDataItem
    {
        [Required]
        public string DataFieldValue { get; set; }
        public bool IsRequired{ get; set; }
    }

验证工作完美!

谁能给我提示吗?或者告诉我我做错了什么?

谢谢!

There are certain properties in my application that I need to set dynamically whether they are required or not, therefore I can not use the [Required] atribute of Data Annotations.

Maybe this is not the best way to achieve what I want. So I will accept glady anny suggestions in that regard.
I have overridden the DataAnnotationsModelMetadataProvider with:

public class DynamicFieldsMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        {
            if (containerType == null)
                throw new ArgumentNullException("containerType");

            if (!typeof(DynamicFieldDataItem).IsAssignableFrom(containerType))
                foreach (var metadataProperty in base.GetMetadataForProperties(container, containerType))
                    yield return metadataProperty;
            else 
                foreach (var metadataProperty in base.GetMetadataForProperties(container, containerType))
                {
                    var dynamicField = (DynamicFieldDataItem)container;
                    if (metadataProperty.PropertyName == "DataFieldValue")
                        metadataProperty.IsRequired = dynamicField.IsRequired;
                    yield return metadataProperty;
                }

        }
}

This is just a concept test, once I make it work, I will change it to something dynamic and more object oriented, but so far, with just being able to set the MetadataModel for the property DataFieldValue to IsRequired = true I can get going.

With this I successfully set in a dynamic way the IsRequired property in true (I thought, with this would be enough!), and when I debugg in my view:

@Html.EditorFor(model=>model.DataFieldValue)

The property DataFieldValue is declared like this:

    public class DynamicFieldDataItem
    {
        public string DataFieldValue { get; set; }
        public bool IsRequired{ get; set; }
    }

I can see that the Metadata, has the property IsRequired in true, but when the "DataFieldValue" is rendered the "validator" is not there, and of course the validation doesn't work.

To make sure that there wasn't a problem on the configuration of my project, I checked web.config and the inclusion of the javascripts for validation, all is properly configured. What's more, If I add the attribute Required to my property, like this:

public class DynamicFieldDataItem
    {
        [Required]
        public string DataFieldValue { get; set; }
        public bool IsRequired{ get; set; }
    }

The validation works perfectly!

Can anyone give me a hint? Or tell me what i am doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我入戏太深 2025-01-11 00:28:46

对于高级验证,我建议您查看 FluentValidation for MVC

http://fluentvalidation.codeplex.com/wikipage ?title=mvc

您可以使用 NuGet 将其安装到您的项目中,并且可以使用它创建自定义验证类。

For validations that advanced I recommend you look at FluentValidation for MVC

http://fluentvalidation.codeplex.com/wikipage?title=mvc

you can install it into your project using NuGet, and you can create custom validation classes with it.

看春风乍起 2025-01-11 00:28:46

我相信您需要一个自定义 ModelMetadataProvider 和一个自定义 ModelValidatorProvider 才能正常工作。根据我的经验,他们似乎没有互相利用,而且似乎都设置了不同的验证。

I believe you need both a custom ModelMetadataProvider and a custom ModelValidatorProvider for this to work. From my experience they don't seem to leverage each other and both seem to setup different validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文