I want to use DataAnnotations in MVC4 for valid value only for numeric but greater than 0.
1 Answer
You could use the [Range] attribute:
[Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
public int Value { get; set; }
11 Comments
Darin Dimitrov
It has not worked is not a very precise problem description. At least not one that allows me to help you further. Because this approach has worked perfectly fine for me. Maybe if you explained what specific problems you encountered with the Range attribute we could discuss further.
Hemant Soni
I have written this code, but not working [Required] [Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")] public virtual int EstimateHours { get; set; }
Darin Dimitrov
What does not working mean??????????? You get an compile-time error? You get a runtime error? You don't get the expected result? Your computer crashes? Apples start falling from the sky?
Hemant Soni
error message is not showing if i press enter after enter 0 in this field
Hemant Soni
I want to ask one question more, can we show or share image here?
|