1

I'm just learning MVC 3.0 and ValidationMessageFor works great, but each one of those is attached to a specific user input.

How would I go about creating a general validation error?

I.E.: A number of fields are supposed to add up to 100. Each one individually has a possible range of 0-100, but combined should also not cross 100.

I want to notify the user of this error.

1 Answer 1

2

You can always insert a record manually like so:

 if(numbers > 100)
   ModelState.AddModelError("key", "Message I want to show");

When you use

Html.ValidationSummary()

It will show up.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Tejs, what is the "key" isn't that linked to an attribute of the model? I tried calling "random" but the error wouldn't display. Thanks!
It's a dictionary under the hood. The 'key' is normally the property name.
Thanks Tejs for the help, but it doesn't seem to be working. Even when ValidationSummary is true it my separate message still doesn't show.
Also, is there a way to do this in the Model itself?
You can also use Html.ValidationMessage("key") to display an inline message wherever you want on the page, if you used the ModelState.AddModelError("key", "Message I want to show"); functoinality.

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.