简体   繁体   English

模型验证不适用于派生类。 即使我已经提到了已知的类型

[英]Model validation is Not working for the derived classes. even though i have mentioned the known types

I am using asp.net core 2.2 and model validation for server side validation.我正在使用 asp.net core 2.2 和模型验证进行服务器端验证。 Its working fine except for known types.除了已知类型外,它的工作正常。

this is my class structure这是我的班级结构

    //Main Class

    [DataContract]    
    [KnownType(typeof(SubClass2))]    
    [KnownType(typeof(SubClass1))]    
    public partial class MainCass : Base    
    {     
    //properties comes here     
    }     

    //Sub Classes    

    [DataContract]     
    public partial class SubClass1 : MainCass    
    {    
    //properties comes here    
    }    

    [DataContract]    
    public partial class SubClass2 : MainCass    
    {    

    [DataMember]    
    [CustomRequired(ErrorMessageResourceType = typeof(ErrorMessages),  
    ErrorMessageResourceName = "FieldRequired", Caption = "name required")]    
    public string Name  {get; set; }   

    }    

//this is my request model
    [DataContract]    
    public partial class request:Base    
    {    
    [DataMember]    
    public List<MainCass> MainCassList  {get; set; }    
    }    

now the validation attribute of Name in SubClass2 is not getting called.现在没有调用 SubClass2 中 Name 的验证属性。 From UI I am sending type Subclass2.从 UI 我发送类型 Subclass2。

The model binder does not support polymorphism.模型绑定器不支持多态。 It creates the literal type(s) of the model and any related sub-models.它创建模型和任何相关子模型的文字类型。 Then, it attempts to bind the request body to these types.然后,它尝试将请求正文绑定到这些类型。 It will not infer derived types.它不会推断派生类型。

In other words, it sounds like you're sending instances of SubClass1 and SubClass2 as part of your MainClassList property.换句话说,听起来您将SubClass1SubClass2实例作为MainClassList属性的一部分发送。 However, the model binder is going to create all of these as MainClass because that's the type that's defined.但是,模型绑定器将创建所有这些作为MainClass因为这是定义的类型。 Any posted data specific to SubClass1 or SubClass2 will simply be discarded, and in the end, all you have is instances of MainClass .任何特定于SubClass1SubClass2发布数据将被简单地丢弃,最后,您所拥有的只是MainClass实例。 As such, of course no specific validation on SubClass1 or SubClass2 is being run, because you have no instances of SubClass1 or SubClass2 .因此,当然没有对SubClass1SubClass2运行特定验证,因为您没有SubClass1SubClass2实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模型验证 .Net Core 2.0 Web Api 不工作 - Model Validation .Net Core 2.0 Web Api Not working 远程验证模型绑定在dotnet core 2.0的剃须刀页面上不起作用 - Remote validation model binding not working on a razor page in dotnet core 2.0 即使我尝试投射到具有匹配属性的对象,也无法投射类型为Newtonsoft.Json.Linq.JObject的对象 - Unable to cast object of type Newtonsoft.Json.Linq.JObject even though I am trying to cast to an object with matching properties Autofac拦截器不适用于某些类 - Autofac interceptor not working for some classes 我需要激活.NET Core MVC模型验证还是默认激活? - Do I need to active .NET Core MVC Model Validation or is it activated by default 在实体框架模型类中实现加密 - Implementing encryption in entity framework model classes 如何在自定义验证属性中获取模型实例 - How to get model instance in a custom validation attribute Asp.Net Core中用于子类的自定义模型活页夹 - Custom Model Binder In Asp.Net Core for Sub Classes 如何为WebAPI和MVC .netcore 2.0提供两种不同类型的用户 - How to have two different types of users for WebAPI and MVC .netcore 2.0 ASP NET Core 2模型验证不正确 - ASP NET Core 2 Model Validation Incorrect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM