1

I'm using the @Html.ValidationMessageFor helper method for validation but the messages are displayed only when I declare Html.ValidationSummary.
Is there a way to show the error messages next to the elements instead of a Validation summary?

<fieldset>
    <legend>Registration Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName, new { id = "registerName" })
        </li>
        <li>
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password)
            <div>@Html.ValidationMessageFor(m => m.Password)</div>
        </li>
        <li>
            @Html.LabelFor(m => m.ConfirmPassword)
            @Html.PasswordFor(m => m.ConfirmPassword)
            <div>@Html.ValidationMessageFor(m => m.ConfirmPassword)</div>
        </li>
        <li>
            <input type="submit" value="Sign up" />
        </li>
    </ol>
    @*@Html.ValidationSummary()*@
</fieldset>

Thanks!

5
  • 2
    Put some example code Commented Dec 20, 2013 at 10:58
  • 1
    Code posted. Only when the ValidationSummary is present (not marked as comment) the messages are shown Commented Dec 20, 2013 at 12:07
  • Have you include unobtrusive and validate scripts in your page Commented Dec 20, 2013 at 12:15
  • could you post complete code along with Form? Commented Dec 20, 2013 at 12:36
  • 1
    I have included these scripts. This is the intranet template in visual studio. I just commented the validation summary. Commented Dec 20, 2013 at 12:37

1 Answer 1

2

@Yaron,

Looking at your code, it all looks good. You don't need the Validation Summary for the validation to work correctly. But you do need the jQuery libraries included.

If you look at a stock Internet MVC Application, you will see at the bottom of the Login page this code:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

This will access the jqueryval bundle from the BundleConfig file in the App_Start folder. Assuming you have all of these in your application, that block of code should fix your problem.

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

4 Comments

Oh! What an embarrassment! I didn't notice that these scripts are rendered only when the user is authenticated so all the validations before that are not displayed as intended!!! LOL. Thanks!
No problem at all. Glad I could help you out. If you found my answer helpful, please take a moment to +1 my answer. Thanks!
I don't have enough reputation yet.. sorry mate
Try now :) I helped you out a bit.

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.