Repository navigation
Description
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
Activity
javiercn commentedon Apr 14, 2020
@RichardBr thanks for contacting us.
This is not a feature that we currently support AFAIK.
/cc: @SteveSandersonMS @pranavkm
nssidhu commentedon Apr 23, 2020
This is much needed as in case of scaff folding it just delete and recreates the class again, we would have to re-write everything again. please consider to implement it, need for real life project
mkArtakMSFT commentedon Apr 24, 2020
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
wmgdev commentedon Jun 10, 2020
Agree, It would be really nice if MetadataType worked.
It would allow us to add data annotations for validation to Models generated using Scaffold-DbContext
MattSturtivant commentedon Jul 7, 2020
Just wanted to add a +1 for this - while we can setup a task to mix-in our data annotations after scaffolding for now, I feel this is a regression from MVC.
Gambero81 commentedon Aug 31, 2020
this is a must have feature, especially in projects with automatic class generations like Entity Framework, to allow adding additional validation or business rules.
Hope it will be implemented as soon as possible
IanKemp commentedon Sep 7, 2020
Those "high-priority" features won't matter if big corporates decline to use Blazor because its model-binding support sucks.
navidzehi commentedon Oct 10, 2020
This is really an important issue. One of the main advantages of Blazor versus frameworks like Angular is the rapid development due to capability of reusing the 'server code' for validation or generation of models in the client. @mkArtakMSFT
SteveSandersonMS commentedon Dec 17, 2020
I've just been checking this, and I don't think this issue is Blazor-specific. I think it's a more general point that
System.ComponentModel.DataAnnotations.MetadataTypeAttributedoesn't actually do anything to influence the DataAnnotations validation result.To repro my findings, create a new .NET Core Console Application (so that's neither Blazor nor WebAssembly), containing:
I would have expected this to say
isValid: Falsebut actually it saysisValid: True. I can only get it to respect the[Required]attribute by putting it directly ontoPerson, notPersonMetadata. Also notice that this has nothing to do withpartialclasses (I'm not using them above).@pranavkm Do we have any contacts who work on DataAnnotations? This seems like a weirdness that a lot of people have complained about all over StackOverflow etc. I can't tell whether it used to work in the past (e.g. on .NET Framework) since some people say it does if you use
TypeDescriptor.AddProviderTransparent, but it certainly doesn't appear to work now.Gambero81 commentedon Jan 5, 2021
news on this issue?
DiegoVenancioVieira commentedon Jan 5, 2021
Nothing, there are many other priorities. This is just one more in the backlog list. Waiting too
27 remaining items
eerhardt commentedon May 28, 2021
Until the change is merged into
main, my recommendation would be to pull the change locally and build the file locally.rahul7720 commentedon Jun 19, 2021
Hi Guys,
Any updates on this?
Xaeco commentedon Jul 8, 2021
Still waiting on my pull request to be reviewed. No further progress.
Xaeco commentedon Jul 25, 2021
Update. Option 1 has been approved. Not sure if it will land in a .NET 6 preview or the final release.
Xaeco commentedon Jul 25, 2021
@ajcvickers any chance this fix could be back-ported to .NET 5?
If so what is the process?
rahul7720 commentedon Jul 25, 2021
@Xaeco Any chance if you can build an unofficial NuGet package / Dll and release it for testing purposes?
bricelam commentedon Jul 27, 2021
See also (and upvote) dotnet/efcore#14544
Add validation for MetadataType attribute - Issue #46678 (#51772)