0

I have the following view:

@model MyProject.Models.RegisterViewModel

<div class="content">
<div class="wrapper">
    <h2>E-Bill Saver Registration</h2>
    <hr />

    <div class="columns top-gap">
        <div class="first cell width-6 dark-blue-headings">
            @using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
                @*@Html.ValidationSummary("", new { @class = "text-danger" })*@
                <div class="form-horizontal">                        
                    <div class="form-group">
                        @Html.Label("Account Number", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.AccountNo, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.AccountNo, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("MPNumber", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.MPNumber, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.MPRN, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Email Address", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Confirm Email Address", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Password", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Confirm Password", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Register" class="opc_button" />
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>       
</div>

If there are validation errors on any of the fields then the error message is displayed below the label as expected and wanted. On my controller however I have some custom logic errors and I am adding them to the modelstate as follows:

else
                {
                    //This email address is already assigned to a user      
                    ModelState.AddModelError("Email Address : ", "Error! You must enter an email address that is unique to your account.");
                    return View(model);
                }

If I uncomment the validationsummary on my page then it will display the custom server side error but it will also display the single validation errors which I want underneath the labels. Is there any way that I can set the validation summary to ignore the values that are being listed individually and only return server side validation errors?

Jay
  • 3,012
  • 14
  • 48
  • 99
  • 2
    Use `@Html.ValidationSummary(true)` –  Apr 29 '15 at 06:19
  • 1
    possible duplicate of [How to not display ASP MVC ValidationSummary in case when errors already displayed field by field?](http://stackoverflow.com/questions/29847308/how-to-not-display-asp-mvc-validationsummary-in-case-when-errors-already-display) –  Apr 29 '15 at 06:22
  • They share the same dictionary, so no, not out-of-the-box. You would need to replace the ValidationSummary with a custom one. – iCollect.it Ltd Apr 29 '15 at 10:19

1 Answers1

1

The first string parameter of ModelState.AddModelError method is the key later used to bind the message to the model property.
If you want to display that error message next to email label, make first parameter of AddModelError match your property name:

ModelState.AddModelError("Email", "Error! Email already used...");
zed
  • 2,298
  • 4
  • 27
  • 44