Asp.net mvc 防止在模型中继承验证属性

Asp.net mvc 防止在模型中继承验证属性,asp.net-mvc,inheritance,validationattribute,Asp.net Mvc,Inheritance,Validationattribute,我使用的是其他自定义联系人模型类继承的基本联系人模型 public class BaseContactModel { [Required(ErrorMessage = "Firstname is required")] public virtual string FirstName { get; set; } } 基本联系人模型使用验证属性来标记属性是必需的,但在某些情况下,我希望覆盖或停止该属性。这可能吗 public class ContactModel : BaseCon

我使用的是其他自定义联系人模型类继承的基本联系人模型

public class BaseContactModel
{
    [Required(ErrorMessage = "Firstname is required")]
    public virtual string FirstName { get; set; }
}
基本联系人模型使用验证属性来标记属性是必需的,但在某些情况下,我希望覆盖或停止该属性。这可能吗

public class ContactModel : BaseContactModel
{
    [NotRequired]
    public override string FirstName { get; set; }
}
我试图使用一个新的验证属性NotRequired来返回true,但似乎这些属性正在叠加,因此Required&NotRequired正在运行,验证失败

在寻找解决方案时(我找不到),我发现一些不相关的属性具有“继承”属性,但我在System.ComponentModel.DataAnnotations中的本机验证属性中没有看到这一点


这是一个失败的事业吗?我是否需要推出支持禁用继承的自己的版本?非常感谢您的帮助。

这可能是您真正想要新房产的情况

 public class ContactModel : BaseContactModel 
{
        [NotRequired]
        public new string FirstName { get; set; } 
}

如下所示,我创建了一个类
Model
,它继承了另一个
BaseModel
,使用了new关键字,然后验证了每个实例中的一个。据我所见,它们都使用基本属性

为了验证例程的清晰性,我添加了一个控制类
ControlModel

class Program
{
    static void Main(string[] args)
    {
        ValidationTest<Model>();
        ValidationTest<BaseModel>();
        ValidationTest<ControlModel>();

        Console.Read();
    }

    private static void WriteAttributeInfo<T>()
    {
        Console.WriteLine(string.Concat(typeof(T), " attributes:"));
        typeof(T).GetProperties().SelectMany(m => m.GetCustomAttributes(true)).Select(a => { Console.WriteLine(a); return a; }).ToList();
    }

    private static void ValidationTest<T>()
    {
        object obj = Activator.CreateInstance<T>();
        Console.WriteLine(string.Concat(typeof(T), " test: isValid = ", Validator.TryValidateObject(obj, new ValidationContext(obj, serviceProvider: null, items: null), new List<ValidationResult>())));
    }
}

class ControlModel
{
    public string FirstName { get; set; }

    public string Email { get; set; }
}

class BaseModel
{
    [RequiredAttribute]
    public virtual string FirstName { get; set; }

    [RequiredAttribute]
    public virtual string Email { get; set; }
}

class Model : BaseModel
{
    public new string FirstName { get; set; }

    public new string Email { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
验证测试();
验证测试();
验证测试();
Console.Read();
}
私有静态void WriteAttributeInfo()
{
WriteLine(string.Concat(typeof(T),“attributes:”);
typeof(T).GetProperties().SelectMany(m=>m.GetCustomAttributes(true)).Select(a=>{Console.WriteLine(a);返回a;}).ToList();
}
私有静态void ValidationTest()
{
object obj=Activator.CreateInstance();
Console.WriteLine(string.Concat(typeof(T),“test:isValid=”,Validator.TryValidateObject(obj,new-ValidationContext(obj,serviceProvider:null,items:null),new-List());
}
}
类控制模型
{
公共字符串名{get;set;}
公共字符串电子邮件{get;set;}
}
类基模型
{
[必需属性]
公共虚拟字符串FirstName{get;set;}
[必需属性]
公共虚拟字符串电子邮件{get;set;}
}
类模型:BaseModel
{
公共新字符串FirstName{get;set;}
公共新字符串电子邮件{get;set;}
}
控制台应用程序1.模型测试:isValid=False

ConsoleApplication1.BaseModel测试:isValid=False

控制台应用程序1.ControlModel测试:isValid=True


在此示例中,您似乎无法覆盖/隐藏/忽略继承的必需验证(尚未尝试其他验证)属性。

这是否会替换属性?我尝试过使用新的(虚拟财产),但它似乎仍在验证中。我在过去使用过几次这种策略。它应该是一个全新的属性,因此只能激活一个或另一个验证属性。当然,这取决于您如何声明或使用模型变量来确定实际使用的属性。如果它不起作用,你可能想发布一些控制器代码,看看我们是否能找出原因。谢谢你的回复,我今晚会再做一次努力,甚至可能直接在一个网站上尝试。请参阅下面的答案和示例-从这一点来看,它看起来不会玩球:(我讨厌回答和接受自己的问题,但从上面的测试来看,原来的问题似乎无法解决。这真的没有解决办法吗?