To make the "Custom ErrorMessage" feature of ASP.NET Core Model Data Annotations visible, you can follow these steps:
[Required(ErrorMessage = "Please enter a valid email address")]
[EmailAddress(ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
ValidationSummary
helper to display all validation errors for the model:@model MyViewModel
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<div class="form-group">
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
</div>
<button type="submit" class="btn btn-primary">Submit</button>
}
jquery.validate
and jquery.validate.unobtrusive
libraries in your view:<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.min.js"></script>
This will enable client-side validation for your model and display the custom error message if the validation fails.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-01-04 11:00:00 +0000
Seen: 8 times
Last updated: Aug 05 '22
How to retrieve lost messages in ZeroMQ publish/subscribe model?
How can weights be duplicated and altered using pytorch?
What are the steps to visualize a PCL Model accurately using ROS RVIZ?
What could be the reason for the low AUC and ROC scores of my significant model?
What is the process of using a Word2Vec model on a column within a Pandas dataframe?
How to provide parameters to machine learning model objects?
Is the BTYD model adjusted or modified before its implementation?