api - 如何将isValid方法的contextValidation参数从父级传递给子级
问题描述
我看到他们过去曾问过这个问题,只是以间接的方式没有答案,
问题是无法从父级通过子级获取参数
public class ParentModel
{
public int statusBakashaID { get; set; }
public ChildModel Children { get; set; }
//.....
//.....
}
public class ChildModel
{
[myRequired(ErrorMessage = "",OtherProperty ="statusBakashaID")]
public string id { get; set; }
//.....
//.....
}
public class myRequired: ValidationAttribute
{
public string OtherProperty { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperty = validationContext.ObjectType.GetProperty(OtherProperty);
var otherPropertyValue =
(int)otherProperty.GetValue(validationContext.ObjectInstance,null);
if(otherPropertyValue == 1){
//.....
}
return null
}
}
解决方案
推荐阅读
- apache-spark - 在 PYSPARK 中屏蔽的模块。无法访问
- r - 如何根据 Rstudio 中 3 列中的值计算行的频率
- excel - 将表 1 中的列标题链接到表 2 中的父类别以用于报告过滤器
- node.js - 如何在 sequelize 中创建父/子关联?
- swift - 我使用泛型类型并设置了一个 nil 值,但结果是 `nil == nil` 为 false。如何解决这个问题呢?
- asp.net - 如果 Web.Config 保护所有 cookie,我怎么能不保护 Asp.Net 中的一个 cookie?
- java - Apache POI - 在 Excel 工作表中插入逗号(,)
- c# - Linq 尝试在 groupBy 之后使用 Include
- android - 将 Unity Humaniod 导入 Android App 并传递实时数据
- python - 使用 pyautoit 在游戏中移动光标使光标始终向上/向下