Blazor validation not working when using Metadata class!

Hamed Vaziri 136 Reputation points
2021-05-23T08:09:58.107+00:00

I am using blazore Server side. Form validation is not working when using Metadata class. I am using scaffolding and my DB models are oftern automatically generated. If i modify the it wipes out all my Dataannotation validations.

Here is the class that i am using :

public partial class ContactUs_Product_ProductRequests
    {
        [Key]
        public int ProductRequestID { get; set; }
        [StringLength(50)]
        public string CompanyName { get; set; }
        public int? CompanyTypeID { get; set; }
        [StringLength(50)]
        public string AgentName { get; set; }
        [StringLength(50)]
        public string AgentRole { get; set; }
        [StringLength(50)]
        public string AgentTel { get; set; }
        [StringLength(50)]
        public string AgentMobile { get; set; }
        [StringLength(50)]
        public string AgentEmail { get; set; }
        public string Description { get; set; }
        [StringLength(10)]
        public string RequestDate { get; set; }
    }

public class ContactUs_Product_ProductRequestsMetadata
    {
        [Required]
        public string AgentName { get; set; }
    }

    [MetadataType(typeof(ContactUs_Product_ProductRequestsMetadata))]
    public partial class ContactUs_Product_ProductRequests
    {

    }
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,383 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yinqiu Yao-MSFT 236 Reputation points
    2021-05-24T01:59:45.363+00:00

    Hi @Hamed Vaziri

    It seems it is not yet supported for Blazor.

    You can see the github issue.

    MetadataTypeAttribute doesn't influence DataAnnotations validation result

    Best Regards,
    Yinqiu Yao

    0 comments No comments

  2. Hamed Vaziri 136 Reputation points
    2021-05-25T06:28:14.773+00:00

    Thanks for reply
    What's the best practice for this situation?
    Thanks in advance