My Data annotations and some helpers like @validationMessage arent working, please help, it is on [required], [maxlength], etc.
this is my model:
this is my html helper
@Html.LabelFor(c => c.baseNumber)
@Html.TextBoxFor(c => c.baseNumber, new { @class = "form-control", @maxlength = "7", pattern = "[0-9,.,-]+" })
@Html.ValidationMessage("baseNumber", new { @class = "text-danger" })
my model
[Required(ErrorMessage = "Base number is required.")]
[Display(Name = "Base number")]
[Range(1, 9999999, ErrorMessage = "Value length must be of 7.")]
it doesnt work
Script references in _Layout.cshtml
and _ValidationScriptsPartial.cshtml
support client-side validation.Here is an official doc .You need to add <partial name="_ValidationScriptsPartial">
to your view.:
<form>
@Html.LabelFor(c => c.baseNumber)
@Html.TextBoxFor(c => c.baseNumber, new { @class = "form-control", @maxlength = "7", pattern = "[0-9,.,-]+" })
@Html.ValidationMessage("baseNumber", new { @class = "text-danger" })
</form>
@section Scripts{
<partial name="_ValidationScriptsPartial">
}
result:
Or you can copy the script references in _ValidationScriptsPartial.cshtml
to your view:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.