What is DataAnnotations in mvc?

Total Post:96

 1288  View(s)
Rate this:
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.

  1. Post:194

    Re: What is DataAnnotations in mvc?

    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";
                        required: true,
                        min: mi,


      Modified On Apr-05-2018 11:16:30 PM