Client Side Validations in Asp.net Core MVC

Client Side Validations in Asp.net Core MVC
Data annotations on model properties
[Required]
publicDataTypePropertyName { get; set; }
 
“data-*” attributes in html tags [auto-generated with “asp-for” helper]
<input data-val=”true” data-required=”ErrorMessage” />
 
Import jQuery Validation Scripts
https://cdnjs.cloudflare.com/ajax/libs/ jquery/3.6.0/jquery.min.js
https://cdnjs.cloudflare.com/ajax/libs/ jquery-validate/1.19.3/jquery.validate.min.js
https://cdnjs.cloudflare.com/ajax/libs/ jquery-validation-unobtrusive/3.2.12/ jquery.validate.unobtrusive.min.js
 
Tag Helpers for <img>
<img src=”~/FilePath” asp-append-version=”true” />
<img src=”/FilePath?v=HashOfImageFile” />
 
asp-append-version
Generates SHA512 hash of the image file as query string parameter appended to the file path.
It REGENERATES a new hash every time when the file is changed on the server. If the same file is requested multiple times, file hash will NOT be regenerated.
 
Tag Helpers for <script>
<script src=”CDNUrl” asp-fallback-src=”~/LocalUrl” asp-fallback-test=”object”> </script>
<script src=”CDNUrl”> </script>
<script> object || document.write(“<script src=’/LocalUrl’></script>”); </script>
 
asp-fallback-src
It makes a request to the specified CDNUrl at the “src” attribute.
It checks the value of the specified object at the “asp-fallback-test” tag helper.
If its value is null or undefined (means, the script file at CDNUrl is not loaded), then it makes another request to the LocalUrl through another script tag.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply