Domanda

Here is a class "Cars" and I have 2 attributs (min/max CO2):

    public class Cars
    {
       [Range("MinCO2", int.MaxValue, ErrorMessage = "MaxCO2 < MinCO2")]
       public int MaxCO2 { get; set; }
       [Range(0, "MaxCO2", ErrorMessage = "MaxCO2 > MinCO2")]
       public int MinCO2 { get; set; }
    }

I use the Data Annotation 'Range' to fix the minimum and the maximum value.

But I want to use the car's attribut to fix the minimum or the maximum value.

Or if there is another solution with jQuery, it can be possible.

Thank you so much.

È stato utile?

Soluzione

Try this

Send Mininum value and Maximum value from Controller to View using Model or ViewBag/ViewData

ViewBag.Min=0;
ViewBag.Max=Int32.MaxValue;

In View use jQuery validation to specify min and max validation using passed values. View:

<script language="javascript">
var mi="@ViewBag.Min";
var mx="@ViewBag.Max";
$(document).ready(function()
{
$( "#myform" ).validate({
rules: {
field: {
required: true,
min: mi,
max:mx,
}
};
});
});

http://jqueryvalidation.org/min-method/

Altri suggerimenti

Thank you malkam!

 $("#MinCO2").change(function () {
        var minValue = this.value;
        $("#MaxCO2").rules('add', {
            range: [minValue, Number.MAX_VALUE],
            messages: {
                range: "La valeur MaxCO2 est inférieur à la valeur MinCO2"
            }
        });
    });

    $("#MaxCO2").change(function () {
        var maxValue = this.value;
        $("#MinCO2").rules('add', {
            range: [Number.MIN_VALUE, maxValue],
            messages: {
                range: "La valeur MinCO2 est supérieur à la valeur MaxCO2"
            }
        });
    });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top