如何使用数据注释对模型进行条件验证?
例如,假设我们有以下模型(Person和Senior):
public class Person
{
[Required(ErrorMessage = "*")]
public string Name
{
get;
set;
}
public bool IsSenior
{
get;
set;
}
public Senior Senior
{
get;
set;
}
}
public class Senior
{
[Required(ErrorMessage = "*")]//this should be conditional validation, based on the "IsSenior" value
public string Description
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
以下观点:
<%= Html.EditorFor(m => m.Name)%>
<%= Html.ValidationMessageFor(m => m.Name)%>
<%= Html.CheckBoxFor(m => m.IsSenior)%>
<%= Html.ValidationMessageFor(m => m.IsSenior)%>
<%= Html.CheckBoxFor(m => m.Senior.Description)%>
<%= Html.ValidationMessageFor(m => m.Senior.Description)%> …Run Code Online (Sandbox Code Playgroud) 我正在寻找关于实现执行以下操作的验证属性的最佳方法的一些建议.
模型
public class MyInputModel
{
[Required]
public int Id {get;set;}
public string MyProperty1 {get;set;}
public string MyProperty2 {get;set;}
public bool MyProperty3 {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我想至少使用一个值得到prop1 prop2 prop3,如果prop3是填充它的唯一值,它不应该等于false.我将如何为此编写验证属性(s?)?
谢谢你的帮助!