2

I have a view (skipping redundant code for simplicity):

@model CompetencyModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    @Html.EditorFor(model => model.Results)
}

When I submit, if validation fails inside EditorFor, ValidationSummary shows the list of all validation messages for each item coming from EditorFor.

This screenshot is a view, where EditorFor is a single line with radio buttons:

enter image description here

But I need a single error message just saying that something failed. I tried:

@Html.ValidationSummary(true)
@Html.EditorFor(model => model.Results)

But it doesn't show anything. I tried

@Html.ValidationSummary(true)
@Html.EditorFor(model => model.Results)
@Html.ValidationMessageFor(model => model.Results, "ERROR !!!")

But it doesn't show anything.

What am I doing wrong? Thank you.

Solution This will do the trick.

<style>.validation-summary-errors ul { display: none; }</style>
@Html.ValidationSummary("Can't save, you must answer all questions !!")
monstro
  • 6,254
  • 10
  • 65
  • 111
  • @Html.ValidationSummary(true) should work. Is your form in a loop, by any chance? – Forty-Two Feb 05 '13 at 18:31
  • Nope. If it shows a list of validation messages, it should show a general error message for EditorFor, if I pass (true), but it doesn't. – monstro Feb 05 '13 at 18:33

2 Answers2

3

The ValidationExtensions.ValidationSummary method will always return an un-ordered list of items.

Another person asked this question and choose to use JQuery to hide the items: https://stackoverflow.com/a/10837702/1220302

Another solution you could do is only display the first item in the list using CSS's nth-child pseudo tag. This of course is dependent on the browser version you're using though:

I think something like this:

.validation-summary-errors ul li:nth-child(-n+1) {
    display:none;
}
Community
  • 1
  • 1
anAgent
  • 2,550
  • 24
  • 34
  • i stand corrected. Summary returns ul or nothing as you said. – Dave Alperovich Feb 05 '13 at 19:25
  • But what about: ValidationSummary(HtmlHelper helper, Boolean excludePropertyErrors, String message)?? excludePropertyErrors: true to have the summary display model-level errors only, or false to have the summary display all errors. message: The message to display with the validation summary. – monstro Feb 05 '13 at 19:34
  • Oh, it seems this message only shows when the first parameter is false, like a lable for UL list of messages.... – monstro Feb 05 '13 at 19:45
  • Yes, you are correct. If you don't already have it defined, you'll want to check out the IValidateable Interface if you're looking at doing a model-level validation summary.: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.server.ivalidatable(v=vs.90).aspx. On the validation topic, check out Fluent Validation as well. http://fluentvalidation.codeplex.com/ – anAgent Feb 05 '13 at 19:47
2

I think you can use the following rule to hide all messages except the first:

.validation-summary-errors ul li:nth-child(n+2) {
    display:none;
}
almaceleste
  • 397
  • 4
  • 11