C#模型中包含变量的双范围

本文关键字:范围 变量 包含 模型 | 更新日期: 2023-09-27 18:28:26

我在Model中有两个值,指定最小和最大平衡。

我想知道是否有Model中设置限制的方法,强制值使MinBalance在0和MaxBalance之间,MaxBalanceMinBalanceDouble.MaxValue之间。

目前我有:

    /// <summary>
    /// Gets or sets the min value.
    /// </summary>
    /// <value>
    /// The min value.
    /// </value>
    [Range(0.0, Double.MaxValue)]
    public decimal MinBalance { get; set; }
    /// <summary>
    /// Gets or sets the max value.
    /// </summary>
    /// <value>
    /// The max value.
    /// </value>
    [Range(0.0, Double.MaxValue)]
    public decimal MaxBalance { get; set; }

我想做这样的事情:

    /// <summary>
    /// Gets or sets the min value.
    /// </summary>
    /// <value>
    /// The min value.
    /// </value>
    [Range(0.0, MaxBalance)]
    public decimal MinBalance { get; set; }
    /// <summary>
    /// Gets or sets the max value.
    /// </summary>
    /// <value>
    /// The max value.
    /// </value>
    [Range(MinBalance, Double.MaxValue)]
    public decimal MaxBalance { get; set; }

遗憾的是,后者在使用Range中的变量时给了我一个错误,因为它没有拾取它们。

C#模型中包含变量的双范围

您可以编写一个自定义验证器来完成此工作。