1

I have an ASP.NET Core 8 MVC application, using the bog standard [Required] attribute on a class property. I expected to see the defined error message in my HTML, in the data-val-required attribute on the input.

But it's weirdly showing the namespace instead?

Here is my class:

namespace Test.Features.Authentication.ViewModels.LoginPageViewModel;

public class LoginPageViewModel : IPageViewModel<LoginPage>
{
    public LoginPageViewModel(){}

    public LoginPageViewModel(LoginPage currentPage)
    {
        CurrentPage = currentPage;
    }

    public LoginPage? CurrentPage { get; set; }

    [Required(ErrorMessage = "Required Message Here")]
    [Display(Name = "Email Address")]
    public string? EmailAddress { get; set; }

    [Required(ErrorMessage = "Required Message Here")]
    [Display(Name = "Password")]
    public string? Password { get; set; }
}

This is the Razor view:

<form class="form-inline" asp-controller="LoginPage" asp-action="Index" method="post">

    @Html.AntiForgeryToken()

    <div>
        <label asp-for="EmailAddress">@Model.CurrentPage.EmailAddressLabel</label>
        <input asp-for="EmailAddress">
        <span asp-validation-for="EmailAddress"></span>
    </div>

    <div>
        <label asp-for="Password">@Model.CurrentPage.PasswordLabel</label>
        <input asp-for="Password" type="password" />
        <span asp-validation-for="Password"></span>
    </div>

    <button type="submit">
        Login
    </button>
</form>

And this is the rendered HTML:

<input type="text" data-val="true" data-val-required="Test.Features.Authentication.ViewModels.LoginPageViewModel.EmailAddress-Required" id="EmailAddress" name="EmailAddress" value="" class="input-validation-error" aria-describedby="EmailAddress-error" aria-invalid="true">

I've never encountered this before... Any help is appreciated

New contributor
user31923066 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.