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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:36:08+00:00 2026-06-15T09:36:08+00:00

I have a view model that implements IValidatableObject that contains a string and a

  • 0

I have a view model that implements IValidatableObject that contains a string and a collection of another view model, something like this:

public sealed class MainViewModel
{
    public string Name { get; set; }
    public ICollection<OtherViewModel> Others { get; set; }
}

My validation checks each object in Others against different rules using the contract provided by IValidatableObject:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    foreach (var other in this.Others)
    {
        // validate or yield return new ValidationResult
    }
}

Because of the complex structure of the real MainViewModel I have had to create a custom model binder which re-builds the model and assigns POST data to the relevant components. The problem that I’m getting is that nothing is getting validated resulting in validation errors at the context level as it violates certain database constraints and I’m not sure what I’m doing wrong – I assumed that ModelState.IsValid would invoke the Validate method on my view model but it doesn’t seem to go down that way.

My model binder looks like this:

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    int modelId = (int)controllerContext.RouteData.Values["id"];

    // query the database and re-build the components of the view model

    // iterate the POST data and assign to the model where necessary

    // should I be calling something here to validate the model before it's passed to the controller?

    return model;
}

Any help appreciated!

Validator.TryValidateObject

OK, seems I’m a little closer. I can now get my IValidatableObject method to run by adding the following to my custom model binder:

var validationResults = new HashSet<ValidationResult>();
var isValid = Validator.TryValidateObject(model, new ValidationContext(model, null, null), validationResults, true);

Seems that Validator.TryValidateObject invokes the validation method and setting the last parameter to true causes it to validate all properties. However, I’m now stuck with getting the validationResults to the controller so they can be used in a meaningful way.

  • 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-15T09:36:09+00:00Added an answer on June 15, 2026 at 9:36 am

    I should have realised that I could use the ModelState.AddModelError through a custom binder, I’ve managed to get this working correctly now by adding the following to my custom model binder before returning the model to the controller:

    var validationResults = new HashSet<ValidationResult>();
    var isValid = Validator.TryValidateObject(model, new ValidationContext(model, null, null), validationResults, true);
    if (!isValid)
    {
        foreach (var result in validationResults)
        {
            bindingContext.ModelState.AddModelError("", result.ErrorMessage);
        }
    }
    
    return model;
    

    This now returns a list of all errors to my page and the ModelState.IsValid check on my controller action is now returning false.

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

Sidebar

Related Questions

I have a view model that looks like this public class ViewModelRound2 { public
I have a Django Model that implements a time range, like this: class Period(models.Model):
I have a view model that implements IValidatableObject and also has several validation attributes.
I have a view model that contains a Product class type and an IEnumerable<
I have a model that gets it's view-count updated after it's viewed. This is
I have a simple property on my view model that is of type string.
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a view model that is displayed in a view that uses uploadify.
I am using javascript unobtrusive validation. I have a view model that I am
I have a view with a model that has several fields. When I render

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.