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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:46:05+00:00 2026-05-28T14:46:05+00:00

How do I manually do the work of the DataAnnotationsModelValidatorProvider after disabling automatic validation

  • 0

How do I manually do the work of the DataAnnotationsModelValidatorProvider after disabling automatic validation application wide (a la https://stackoverflow.com/a/6111442/236255). I ask because

  1. I like how it works and want to leverage its functionality, but
  2. I typically need to do business validation that needs to talk to the database in addition to the usual DataAnnotations-based validation. I could create and dispose of my L2S DataContext within the IValidatableObject.Validate method, but that seems to go against the DI pattern of injecting services and data contexts into my controllers (fighting my functional programmer temptation to write my services as static methods that create and dispose of data contexts themselves!).

Note that I don’t use any of the MVC client validation features (I do all POSTs as ajax, so I have never seen any real benefit to client side validation).

  • 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-28T14:46:06+00:00Added an answer on May 28, 2026 at 2:46 pm

    First, Validate:

    var validation = new List<ValidationResult>();
    
    bool valid = Validator.TryValidateObject(value, new ValidationContext(value, null, null), validation, validateAllProperties: true);
    

    Then, copy the results to ModelState:

    foreach (var item in validation) {
       IList<string> memberNames = item.MemberNames.ToArray();
    
       if (memberNames.Count > 0) {
    
          for (int i = 0; i < memberNames.Count; i++)
             controller.ModelState.AddModelError(memberNames[i] ?? "", item.ErrorMessage);
    
       } else {
          controller.ModelState.AddModelError("", item.ErrorMessage);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://msdn.microsoft.com/en-us/library/ms179254.aspx Will this work for all databases or must I manually use on every
I'm using netbeans 6.9.1 to work on a php project, how do I manually
I start an android service manually which does some data collection work. Suppose if
When manually generating a JSON object or array, it's often easier to leave a
I need to manually migrate modified stored procedures from a DEV SQL Server 2005
I am having problems manually looping through xml data that is received via an
I just got through manually removing the bin\ and obj\ folders from around 30
How would you manually trigger additional team builds from a team build? For example,
When I do this manually public class AdventureWorks : DataContext { public AdventureWorks(string connection)
I am manually creating the equivalent lambda: var function = p => p.Child.Any(c =>

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.