0

I am writing an mvc web app and I'm trying to check to see if there are any errors in the Html.ValidationSummary to determine if I need to display the div that handles the messages.

 @if(Html.ValidationSummary() != null)
  {
    <div class="registration-val-summary">
      <label class="registration-val-label">
        Please correct the following issues with your registration:
      </label>
      @Html.ValidationSummary()
    </div>
  }

This is the error that I receive:

Unexpected "if" keyword after "@" character. Once inside code, you do not need to prefix constructs like "if" with "@".

From what I read in this post: Unexpected "foreach" keyword after "@" character It is due to the "Html" that I have inside my code. I understand the error but I do not know how to get around it. I want to avoid displaying the div that wraps the ValidationSummary unless there are actually errors to display otherwise the message about correcting your error shows up and displaces my form.

How do I hide this if ValidationSummary has no messages to display?

Community
  • 1
  • 1
ErocM
  • 4,505
  • 24
  • 94
  • 161
  • The error is because your `@if(Html.ValidationSummary() != null) { ... }` block is inside another code block (probably because its inside `@using (Html.BeginForm())` - just remove the `@`. But the code makes no sense since it can never be `null` (the method returns a `MvcHtmlString`) –  Nov 16 '16 at 21:46

2 Answers2

1

You are recieving that error because you are rendering the partial view rasor code from Html.ValidationSummary() into your if statement. The compiler then sees any @ symbols the validationSummary renders and fails.

A simple check to see if there are validation errors is to do @if(!ViewData.ModelState.IsValid)

@if(!ViewData.ModelState.IsValid)
  {
    <div class="registration-val-summary">
      <label class="registration-val-label">
        Please correct the following issues with your registration:
      </label>
      @Html.ValidationSummary()
    </div>
  }
Keith.Abramo
  • 6,952
  • 2
  • 32
  • 46
  • Ty for the response. I copied and pasted this code exactly how you have it and I get the same message "Unexpected "if" keyword after "@" character." – ErocM Nov 16 '16 at 17:11
  • The `@Html.ValidationSummary()` should be outside the `if` block, other wise its never rendered initially and cannot be used for client side validation –  Nov 17 '16 at 07:09
0

Html.ValidationSummary() is displayed IF and ONLY IF there validation errors on the page.

What you are trying to do is already achieved by the Html.ValidationSummary() helper and it returns an HTML div element that contains an unordered list of all validation error messages from the model-state dictionary [Source]

Html.ValidationSummary() renders a partial view, but you can customize/extend the view by following this answer

In your case, you might want to put the following code inside the customized view that you will create

<div class="registration-val-summary">
  <label class="registration-val-label">
    Please correct the following issues with your registration:
  </label>
</div>
Community
  • 1
  • 1
Mihir Kale
  • 1,028
  • 1
  • 12
  • 20
  • @MikirKale Ty for the response. I originally did put that code in there but the "Please correct..." shows up regardless if there is a message or not. – ErocM Nov 16 '16 at 17:03