Hi @zbx888,
Use the asp-for
together with StringLength
data annotation will generate the html in the browser like below:
<textarea cols="100" rows="2" data-val="true" data-val-length="Comments must be less than 250 characters." data-val-length-max="2" id="SRM_NotFindingsConsistent"
maxlength="250" name="SRM.NotFindingsConsistent" spellcheck="false" data-ms-editor="true"></textarea>
You can see it generates the html with maxlength
attribute. This attribute will limit the user type amount of characters without popping up the error message.
If you want to display the error message, an alternative way is to avoid using asp-for
tag helper, just use the name
attribute:
<textarea name="SRM.NotFindingsConsistent" cols="100" rows="2"></textarea>
<span asp-validation-for="SRM.NotFindingsConsistent" class="text-danger"></span>
Judge the ModelState
in the backend and return page, it will display the error message when post back:
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
//....more code
return Page();
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena