Share via

Why Doesn't View validation show the error message?

Coreysan 1,866 Reputation points
2023-04-21T00:44:58.1733333+00:00

Here's my code for the model and the view:

public class MyName
{
    [Required(ErrorMessage = "Enter Full Name")]
    public string Name { get; set; } = string.Empty;
}

***

@model MyName

<div class="container">
    <div class="col-6">
        <form method="post" asp-controller="Home" asp-action="PostIndex">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>

            <input type="submit" class="btn btn-primary" value="Submit" />
        </form>
    </div>
</div>

When I run the app, I leave the input box alone (empty) and click submit. I'm looking to see the error message "Enter full name" show up on the view. But it doesn't. Am I doing something wrong, or forgot something?

Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author

SurferOnWww 6,016 Reputation points
2023-04-21T01:16:17.63+00:00

Please add the following code at the last line of View and try:

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.