WHAT IS DATAANNOTATIONS IN MVC?

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
 1085  View(s)
Ratings:
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. Kamlakar Singh

    Post:194

    Points:1396
    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";
        $(document).ready(function(){
            $('#myform').validate({
                rules:{
                    field:{
                        required: true,
                        min: mi,
                        max:mx,
                    }
                };
            });
        });
    </script>

     

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!