0

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.

1 Answer 1

2

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 )
Sign up to request clarification or add additional context in comments.

Comments

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.