2

Is it possible to add a specific error message to a field in a view? Currently I have validation on the model to check whether a field is empty (ValidationMessageFor<>) and I am using validation summary for specific errors. What I'm wondering is whether the specific error message created for the summary can replace the ValidationMessageFor<>?

Model:

 [Required]
 public string SchoolCode { get; set; }

View:

@Html.ValidationSummary("", new { @class = "text-danger" })
@Html.EditorFor(m => m.SchoolCode, new { htmlAttributes = new { @class = "form-control", @placeholder = "School Code" } })

@Html.ValidationMessageFor(model => model.SchoolCode, "", new { @class = "text-danger" })

Controller:

var schoolExists = RepositoryHelper.GetSchoolByCode(model.SchoolCode);
if (schoolExists == null)
{
    ModelState.AddModelError(string.Empty, @"No school exists with this code");
    return View(model);
}
if (ModelState.IsValid)
{
    //do other stuff
}

So if the model state isn't valid it returns the model error, but if an incorrect code is entered it adds "No school exists with this code" to the validation summary. Could I replace the school code model error with the school code summary error?

1
  • In future please format your code for better reading. Commented May 9, 2016 at 12:33

2 Answers 2

6

You need to specify the property name in the AddModelError() method

ModelState.AddModelError("SchoolCode", "No school exists with this code");

so that it will be displayed in the placeholder identified by @Html.ValidationMessageFor(m => m.SchoolCode, ... })

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

Comments

0

Yes it is possible to override the error messages. All that you need to do is to reference the item in the ModelState.AddModelError method call and to specify the error message text.

You can override the following error messages:

  • The default error message if no error message is specified
  • The error message that you specified

The generated HTML output for your textbox will look something like this:

<input
    class="input-validation-error form-control text-box single-line"
    data-val="true"
    data-val-required="Source code is required"
    id="SchoolCode"
    name="SchoolCode"
    placeholder="School Code"
    type="text"
    value="" />

It is important to note the values of the id and name propeties. They have been set to SchoolCode. When you need to override the error message you need to make use of the value SchoolCode so that you know which HTML element you are referencing/working with.

So adding a custom error message based on some other criteria you need to change your code to look like the following:

if (schoolExists == null)
{
     ModelState.AddModelError("SchoolCode", "No school exists with this code");

     return View(model);
}

I hope this helps.

Comments

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.