Reputation: 26919
Just learning about Validation, so I had defined a property on my model like this:
public string NumberOfThings { get; set; }
and a HTML for it like this:
@Html.TextBoxFor(t => t.NumberOfThings , new {style = "width: 10%", @class = "form-control"})
Now I want to add validation to it, the data is saved as String in Database schema. My validation logic want it to say any number larger than 0 is OK.
So I learned about validation and that I can do things like this:
[StringLength(3, MinimumLength = 1)]
public string SelectedQuestions { get; set; }
and then was aboe to see that oh my ModelState
is not valid.
But since I am new to this I couldn't figure out what is the correct validation I should annotate my property with so that it says any number larger than 0 is OK.
Upvotes: 0
Views: 28
Reputation: 6335
a number of something is an int not a string.
you can do something like :
[Range(1, int.MaxValue, ErrorMessage = "some message")]
public int NumberOfThings { get; set; }
in your html you then use ValidationFor and it will look at what you have defined in your model:
@Html.EditorFor(model => model.NumberOfThings )
@Html.ValidationMessageFor(model => model.NumberOfThings )
Upvotes: 2