Skip to content

MetadataTypeAttribute doesn't influence DataAnnotations validation result #46678

Description

@RichardBr

Update by @SteveSandersonMS: please see the comment #46678 (comment) for a description of the underlying issue/question.

Please ignore the parts of this thread that are specific to Blazor. That's just how the issue was originally reported, but it looks like the underlying concern is about DataAnnotations itself.


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

Further technical details

  • Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.300-preview-015048
 Commit:    13f19b4682

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.300-preview-015048\

Host (useful for support):
  Version: 3.1.3
  Commit:  4a9f85e9f8

.NET Core SDKs installed:
  3.1.101 [C:\Program Files\dotnet\sdk]
  3.1.201 [C:\Program Files\dotnet\sdk]
  3.1.300-preview-015048 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions