Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove list from validation summary

I added ValidationSummary Html helper for my View Model class which has 5 required fields. And it works got nice red words missing 1, missing 2 etc. But I need to display just one message not five of them (something like: "Your input is not valid."). Can this be done with ValidationSummary?

like image 265
1110 Avatar asked Sep 11 '11 20:09

1110


2 Answers

You have two options (at least):

Either use the validation summary and exclude property errors:

@Html.ValidationSummary(true, "The input is not valid")

or associate a error message with a custom key in your action:

if (!ModelState.IsValid)
{
    ModelState.AddModelError("myerrorsummary", "The input is not valid");
}

and display it on your page:

@Html.ValidationMessage("myerrorsummary")
like image 123
Rune Avatar answered Oct 11 '22 21:10

Rune


You can try skipping the helpers if all you want to do is simply display a message if the ModelState is not valid. Simply check the ModelState within ViewData and that should work.

@if (!ViewData.ModelState.IsValid)
{
    <p>Your input is not valid.</p>
}
like image 42
Amadiere Avatar answered Oct 11 '22 21:10

Amadiere