Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:35:07+00:00 2026-06-01T18:35:07+00:00

I am trying to attach an attribute to the particular property in the this

  • 0

I am trying to attach an attribute to the particular property in the this case the FirstName but the problem is in this code it is attaching to the birthday datetime property as well . what might be the problem with this

 public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
{
    protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
    {
        if ( metadata.PropertyName == "FirstName")
            attributes = new List<Attribute>() { new RequiredAttribute() };

        return base.GetValidators(metadata, context, attributes);
    }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
}



 protected void Application_Start()
    {

        //ModelValidatorProviders.Providers.Clear();
        //ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider()); 
        ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

can someone can explain how GetValidators is working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:35:08+00:00Added an answer on June 1, 2026 at 6:35 pm

    Your problem has nothing to do with your GetValidators method.

    Value types like (int, decimal, DateTime, etc.) are required by default. Because otherwise the model binder cannot set their values if they are not sent with the request.

    So you need to change your Birtday property to nullable if you don’t want to be required:

    public class User
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime? Birthday { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to attach the Netbeans profiler to my Java project but it
I am trying to attach a video in my mms application. But the application
I have a question related to this one : I'm trying to attach an
I'm trying to attach a file to an outgoing email but the attachment size
I am trying to attach 'click' events to all elements of a particular class.
HiI am trying to attach the where clause to sql statement like this public
I am trying to attach events that would manipulate JQM components, but once I
I am trying to store an object as an attribute in another object but
ClientID property of Dynamic CheckBox Problem: I need to attach some javascript function for
I'm trying to attach an entity in LINQ to SQL but it throws the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.