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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:00:37+00:00 2026-05-30T02:00:37+00:00

I have a custom model binder inheriting from DefaultModelBinder. What I want it to

  • 0

I have a custom model binder inheriting from DefaultModelBinder. What I want it to do is to set a property on the model, that can’t be resolved by the DefaultModelBinder. It looks like this:

public class FooModelBinder : DefaultModelBinder  {
  public override void BindModel(ControllerContext controllerContext, ModelBindingContext modelBindingContext) 
  {
    var model = base.BindModel(controllerContext, bindingContext);
    ((IFooModel)model).Bar = GetBarFromSomewhere();

    return model;
  }
}

However, since the Bar property in IFooModel cannot be null and I’m using FluentValidation with a rule saying that, the ModelState will be invalid after I’ve called base.BindModel.

So I would like to either avoid validating the model when calling base.BindModel, or at least clear the errors and re-validate the model after I’ve set the Bar property.

I’ve tried resolving the validators and validating the model, but I can’t seem to get it to actually run the validation, and it doesn’t result in any errors (even when it should):

var validators = bindingContext.ModelMetadata.GetValidators(controllerContext);
foreach(var validator in validators) {
    foreach (var result in validator.Validate(model)) {
        bindingContext.ModelState.AddModelError(result.MemberName, result.Message);
    }
}

After running this before I return model, validators contains a FluentValidationModelValidator, but when I call validator.Validate I don’t get any errors. I have another property on my model which did cause an error when I ran base.BindModel earlier, so I would expect the same error to occur here.

  • 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-30T02:00:38+00:00Added an answer on May 30, 2026 at 2:00 am

    Instead of overriding the BindModel method you could try overriding the BindProperty method:

    public class FooModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Name == "Bar")
            {
                var model = bindingContext.Model as IFooModel;
                if (model != null)
                {
                    model.Bar = GetBarFromSomewhere();
                }
            }
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have one custom model binder that inherits from DefaultModelBinder, where I am
I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the
In my application I have a custom model binder that I set to the
This is my custom model binder. I have my breakpoint set at BindModel but
I have a custom model binder with public override object BindModel(controllerContext, bindingContext) that checks
I have a custom model binder that takes a comma separated list and cleans
I'm using the MVC 2 RC. I have a custom model binder that used
In my custom model binder I use bindingContext.ValueProvider.GetValue(propertyName); I do have [ValidateInput(false)] on the
I'm creating a custom model binder in an Mvc application and I want to
I have in my model a property of a certain type, it's a custom

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.