【问题标题】:How to require the filling of an Entity attribute dependent on a condition - ASP NET Core MVC and Entity Framework如何根据条件要求填充实体属性 - ASP NET Core MVC 和实体框架
【发布时间】: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


【解决方案1】:

我建议您尝试在 cmets 中提到的 FluentValidation。花点时间研究它真的很值得。假设您已经下载了它并想要实施验证。

public class InputModel
{
    public string Email { get; set; }

    public string Password{ get; set; }

    public Item Item { get; set; }

    public Item2 Item2 { get; set; }
}



public class InputModelValidator
    : AbstractValidator<InputModel>
{
    public InputModelValidator() 
    {
        RuleFor(request => request.Item2)
        .NotNull()
        .When(request =>
              request.Item != null
              && !string.IsNullOrEmpty(request.Item.Code)
              && request.Item.Code == "yes")
        .WithMessage("Item2 is mandatory if Item.code == 'yes'");
    }
 }

尽管如此,有很多不同的方法可以做到这一点,但我在我的项目中使用了 FluentValidation。

【讨论】:

  • 我没有注意到你写过你不会使用任何额外的包。是的,所以这意味着我的答案不是要走的路。
【解决方案2】:

首先,在Item2 类中,删除[Required] 属性。然后,在 InputModel 中,您可以实现 IValidatableObject,然后验证值。代码如下:

    public class Item
    {
        [Required]
        public string Id { get; set; }
        [Required]
        public string desc { get; set; }
        [Required]
        public string code { get; set; }
    }

    public class Item2
    { 
        public string Id { get; set; } 
        public string slug { get; set; }
    }

    public class InputModel: IValidatableObject
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
        [Required]
        public Item Item { get; set; }

        public Item2 Item2 { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (Item.code.ToLower() == "yes")
            {
                if(Item2.Id==null)
                    yield return new ValidationResult($"Item2 ID is required!", new[] { "Item2.Id" });
                
                if (Item2.slug == null) 
                    yield return new ValidationResult($"Item2 slug is required!", new[] { "Item2.slug" }); 
            }
        }
    }

截图如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多