Aspnetcore: Blazor validation doesnt work when using MetadataType attribute on classs

Created on 14 Apr 2020  ยท  8Comments  ยท  Source: dotnet/aspnetcore

Describe the bug

When I use the MetadataType attribute (from System.ComponentModel.DataAnnotations) on the class for the purpose of validation I get the following error in the console window
WASM: System.TypeLoadException: Could not resolve type with token 01000027 from typeref (expected class 'System.ComponentModel.DataAnnotations.MetadataTypeAttribute' in assembly 'System.ComponentModel.Annotations, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

Greater details of the error are shown in this file...[console screenshot - validation error](https://user-images.githubusercontent.com/5938313/79199691-42595400-7e2d-11ea-95bc-3c2248d8b60e.png

Please note my sample application does works when I put all validation attributes into main the class.

For clarity:
This approach works...

public partial class PersonViewModel
{
    [Required(ErrorMessage = "First name is required.")]
    public string Fname { get; set; }
}

This approach fails...

public partial class PersonViewModel
{
    public string Fname { get; set; }
}

[MetadataType(typeof(PersonViewModelMetaData))]
public partial class PersonViewModel
{
}

public class PersonViewModelMetaData
{
    (ErrorMessage = "First name is required.")]
    public string Fname { get; set; }
}

To Reproduce

Please run the attached sample blazor application. To see the error please click the navigation link labelled "validation - failure". On the page, just simply press the submit button to see the error.
BlazorAppValidation.zip