Home > DeveloperSection > Forums > What is DataAnnotations in mvc?
ben reitman
ben reitman

Total Post:96

Points:676
Posted on    October-09-2014 12:39 AM

 ASP.NET MVC ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 656  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.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-09-2014 12:47 AM

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>

 


Don't want to miss updates? Please click the below button!

Follow MindStick