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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:21:26+00:00 2026-06-17T05:21:26+00:00

I was under the impression that when binding to a complex model, all public

  • 0

I was under the impression that when binding to a complex model, all public properties were processed and a match binding attempted for each.

I’m trying to resolve a variable naming problem so that a model

class Model {
      public string Foo {get;set;}
      public string FooBar {get;set;}
}

works nicely with a query string like

?foo=foo&foo_bar=foo_bar

Is there a better way than with a custom model binder? In any case, mine doesn’t work. FooBar is simply skipped.

public class StringModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = base.BindModel(controllerContext, bindingContext);

            if (model != null)
                return model;

            var modelName = Regex.Replace(bindingContext.ModelName, "([a-z])([A-Z])", "$1_$2").ToLowerInvariant();

            var value = bindingContext.ValueProvider.GetValue(modelName);

            return value;
        }

    }

Registered with

ModelBinders.Binders.Add(typeof(string), new StringModelBinder());
  • 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-17T05:21:27+00:00Added an answer on June 17, 2026 at 5:21 am

    I was under the impression that when binding to a complex model, all
    public properties were processed and a match binding attempted for
    each.

    No, that’s a wrong impression. The default model binder will attempt to bind only the properties for which you have a corresponding value in the Request. In your case you do not have a corresponding value for the FooBar property so it won’t be bound.

    Actually it would be nice if we could write:

    public class Model
    {
        public string Foo { get; set; }
    
        [ParameterName("foo_bar")]
        public string FooBar { get; set; }
    }
    

    So let’s implement this. We start by writing a base attribute:

    [AttributeUsageAttribute(AttributeTargets.Property)]
    public abstract class PropertyBinderAttribute : Attribute, IModelBinder
    {
        public abstract object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);
    }
    

    and a custom model binder:

    public class CustomModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
        {
            var propertyBinderAttribute = propertyDescriptor
                .Attributes
                .OfType<PropertyBinderAttribute>()
                .FirstOrDefault();
    
            if (propertyBinderAttribute != null)
            {
                var value = propertyBinderAttribute.BindModel(controllerContext, bindingContext);
                propertyDescriptor.SetValue(bindingContext.Model, value);
            }
            else
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }
    }
    

    As you can see this custom model analyzes the metadata of the model and if a property is decorated with an instance of the PropertyBinderAttribute it will use it.

    We will then replace the default model binder with our custom one in Application_Start:

    ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
    

    and all that’s left now is to implement the ParameterNameAttribute binder that we used to decorate our model property with:

    public class ParameterNameAttribute : PropertyBinderAttribute
    {
        private readonly string parameterName;
        public ParameterNameAttribute(string parameterName)
        {
            this.parameterName = parameterName;
        }
    
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var value = bindingContext.ValueProvider.GetValue(this.parameterName);
            if (value != null)
            {
                return value.AttemptedValue;
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under the impression that strings have properties, such as match . Why
I was under the impression that model binding in the ASP.Net Web API was
I was under the impression that using the Resolve method returned a new instance
I was under the impression that all updates to a SQL server database are
I was under impression that rm -r *.xml would remove all file from parent
I'm trying to understand how to use RoutedCommands. I was under the impression that
I was under the impression that all value types inherit from System.ValueType , and
I was under the impression that when we send mail to any domain (i.e.
I'm under the impression that a control in a nested UpdatePanel will cause the
I was under the impression that when you called Flush() in a StreamWriter object

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.