[英]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.换句话说,听起来您将
SubClass1
和SubClass2
实例作为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
.任何特定于
SubClass1
或SubClass2
发布数据将被简单地丢弃,最后,您所拥有的只是MainClass
实例。 As such, of course no specific validation on SubClass1
or SubClass2
is being run, because you have no instances of SubClass1
or SubClass2
.因此,当然没有对
SubClass1
或SubClass2
运行特定验证,因为您没有SubClass1
或SubClass2
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.