0

I have two values in a Model, specifying the minimum and maximum balance.

I want to know if there is a way to set the limitation within the Model that enforces the values to have MinBalance between 0 and MaxBalance, and have MaxBalance between MinBalance and Double.MaxValue.

Currently I have:

    /// <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; }

I wanted to do something like this:

    /// <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; }

Sadly, the latter gives me an error on using the variables in the Range, as it does not pick them up.

EternalWulf
  • 742
  • 9
  • 21
  • 3
    2 Problems, the values passed to attributes need to be constants/literals they cannot be variables and also `Double.MaxValue` is greater than you can hold in a `decimal`. – Ben Robinson Oct 29 '14 at 11:14
  • 1
    See http://stackoverflow.com/questions/3713281/attribute-dependent-on-another-field to create your own attribute like `DependentRange` – artm Oct 29 '14 at 11:18

1 Answers1

0

You can write a custom validator do to this job.