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 6701399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:55:47+00:00 2026-05-26T06:55:47+00:00

What’s a good way to validate a model when information external to the model

  • 0

What’s a good way to validate a model when information external to the model is required in order for the validation to take place? For example, consider the following model:

public class Rating {
    public string Comment { get; set; }
    public int RatingLevel { get; set; }
}

The system administrator can then set the RatingLevels for which a comment is required. These settings are available through a settings service.

So, in order to fully validate the model I need information external to it, in this case the settings service.

I’ve considered the following so far:

  1. Inject the service into the model. The DefaultModelBinder uses System.Activator to create the object so it doesn’t go through the normal dependency resolver and I can’t inject the service into the model without creating a new model binder (besides which, that doesn’t feel like the correct way to go about it).
  2. Inject the service into an annotation. I’m not yet sure this is possible but will investigate further soon. It still feels clumsy.
  3. Use a custom model binder. Apparently I can implement OnPropertyValidating to do custom property validation. This seems the most preferable so far though I’m not yet sure how to do it.

Which method, above or not, is best suited to this type of validation problem?

  • 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-05-26T06:55:48+00:00Added an answer on May 26, 2026 at 6:55 am

    Option 1 doesn’t fit. The only way it would work would be to pull in the dependency via the service locator anti-pattern.

    Option 2 doesn’t work. Although I couldn’t see how this was possible because of the C# attribute requirements, it is possible. See the following for references:

    • Resolving IoC Container Services for Validation Attributes in ASP.NET MVC
    • NInjectDataAnnotationsModelValidatorProvider

    Option 3: I didn’t know about this earlier, but what appears to be a very powerful way to write validators is to use the ModelValidator class and a corresponding ModelValidatorProvider.

    First, you create your custom ModelValidatorProvider:

    public class CustomModelValidatorProvider : ModelValidatorProvider
    {
        public CustomModelValidatorProvider(/* Your dependencies */) {}
    
        public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
        {
            if (metadata.ModelType == typeof(YourModel))
            {
                yield return new YourModelValidator(...);
            }
        }
    }
    

    ASP.NET MVC’s IDependencyResolver will attempt to resolve the above provider, so as long as it’s registered with your IoC container you won’t need to do anything else. And then the ModelValidator:

    public class EntryRatingViewModelValidatorMvcAdapter : ModelValidator
    {
        public EntryRatingViewModelValidatorMvcAdapter(
                ModelMetadata argMetadata,
                ControllerContext argContext)
                    : base(argMetadata, argContext)
        {
            _validator = validator;
        }
    
    
        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            if (/* error condition */)
            {
                yield return new ModelValidationResult
                  {
                    MemberName = "Model.Member",
                    Message = "Rating is required."
                  };
            }
        }
    }
    

    As the provider is retrieved through the IDependencyResolver and the provider has full control over the returned ModelValidators I was easily able to inject the dependencies and perform necessary validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.