【问题标题】:Custom Required attribute - property required depending of parent model value自定义必需属性 - 所需属性取决于父模型值
【发布时间】:2015-10-03 08:18:34
【问题描述】:

我有一个模型 ProductModel 具有: * bool IsNew 属性 * ProductDetailsModel 详细信息属性

public class ProductModel
{
    public bool IsNew { get; set; }
    public ProductDetails Details { get; set; }
}

public class ProductDetails
{
    public string Code { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public int Number { get; set; }
}

ProductDetails 有一些其他属性,例如。代码、类型、描述、编号

我希望仅当 ProductModelIsNew 设置为 true 时才需要 ProductDetailsModel 的 Description 和 Number 属性。

怎么做?

顺便说一句,我在 ProductModel 中有更多自定义类型的属性,我无法将它们的属性移动到单个 ProductModel 中。

【问题讨论】:

  • 这更像是“业务逻辑”,而不是应该在模型中完成的事情。

标签: asp.net-mvc custom-attributes required-field


【解决方案1】:

我在这里找到了答案ASP.NET MVC Conditional validation

这似乎是在 Product 模型中实现验证的最简单方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2019-11-30
    • 2014-04-22
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2017-03-10
    相关资源
    最近更新 更多