How can I automatically mark required field labels with a * (based on [Required] annotation) in a razor view using asp.net core 3.1/5 mvc ?
e.g. so a form would look like:
* = required
Name* :
Age* :
etc
Is there a way to do this with a tag helper?
I'd prefer to do this in the cshtml markup than via JavaScript or jQuery.
This isn't to validate, this is just to inform the user on sight which fields are required, without having to hard code *'s.
You can achieve this goal by customizing taghelper.
Example:
RequiredTagHelper:
Then in your
_ViewImports.cshtml,add:Css:
View:
Test result:
In this case,when you use
<label asp-for="xx"></label>, it will check if your attribute is required, if it is required, then it will add *.