【发布时间】:2021-04-09 11:11:24
【问题描述】:
我希望 Item2 仅在 Item.code = "yes" 时是强制性的。 InputModel 用于Register Register.cshtml.cs。
public class Item
{
[Required]
public string Id{ get; set; }
[Required]
public string desc{ get; set; }
[Required]
public string code{ get; set; }
}
public class Item2
{
[Required]
public string Id{ get; set; }
[Required]
public string slug{ get; set; }
}
public class InputModel
{
[Required]
public string Email { get; set; }
[Required]
public string Password{ get; set; }
[Required]
public Item Item { get; set; }
public Item2 Item2 { get; set; }
}
我打算做这样的事情,IN InputModel
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Item.code=="yes")
{
===> set Item2 Required
}
}
这就是逻辑,谁能帮忙?
【问题讨论】:
-
This answer 到另一个问题可能对您有帮助。
-
请提供更多细节。你现在有什么样的验证?您是否使用其他工具,例如 FluentValidation?
-
没有。我只使用带有 mvc 和实体框架的 asp net core。我正在更改注册页面。我打算应用这条规则。
-
有人有什么建议吗?
标签: asp.net asp.net-mvc asp.net-core