The above solution works, but if you have multiple instances of this field, it would be easier to set it in a model or view model. Be careful with changing the actual model, because they may be unstable when upgrading.
Example: In a model or view model.
[DisplayName("Notes")] [MaxLength(500, ErrorMessage="Maximum length of notes field is 500 characters...")] [DataType(DataType.MultilineText)] public string Notes { get; set; }
To test it on sending, you will need the following in your web.config.
<add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />
Make sure you link somewhere to validation JA files, preferably in Shared / _Layout or on the page itself ...
<script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Finally, make sure you include the validation message in cshtml.
@Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control", rows = "5", @placeholder = "Notes", title = "Enter any notes..." } }) @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
Build a solution and test. Hope this helps someone else find a solution.
Bolts rugger
source share