6

I want to use DataAnnotations in MVC4 for valid value only for numeric but greater than 0.

1 Answer 1

27

You could use the [Range] attribute:

[Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
public int Value { get; set; }
Sign up to request clarification or add additional context in comments.

11 Comments

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.
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; }
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?
error message is not showing if i press enter after enter 0 in this field
I want to ask one question more, can we show or share image here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.