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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:43:03+00:00 2026-06-13T02:43:03+00:00

I’m trying to build a generic model binder that uses reflection to assign properties

  • 0

I’m trying to build a generic model binder that uses reflection to assign properties to a specified type of object retrieved from the BindingContext. So something like this:

public class ModelBinder : IModelBinder
{
    public object BindModel<T, K> (ControllerContext controllerContext, ModelBindingContext bindingContext)
        where T : class, new()
        where K : class
    {
        Type ObjectType = typeof(T);
        Type InterfaceType = typeof(K);
        T obj = new T();

        foreach (var Property in ObjectType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
        {
            Type PropertyType = Property.PropertyType;

            // checks if property is a custom data object
            if (!(PropertyType.GetGenericArguments().Count() > 0 && PropertyType.GetGenericArguments()[0].GetInterfaces().Contains(InterfaceType)))
            {
                Property.SetValue(obj, bindingContext.ValueProvider.GetValue(Property.Name), null);   
            }
        }

        return obj;
    }

Obviously this doesn’t work as it isn’t correctly implementing the IModelBinder interface. Is something like this possible?

Edit:

To expand on why I am doing this, our objects use a lot of different custom objects. For instance, the Class object:

public class Class : ModelBase<Class>
{
    public Class () { }

    public virtual string ClassDescription { get; set; }
    public virtual string ClassName { get; set; }
    public virtual LookUp ClassType { get; set; }
    public virtual double Credits { get; set; }
    public virtual bool Documents { get; set; }
    public virtual LookUp PracticeArea { get; set; }
}

uses the LookUp class:

public class LookUp : ModelBase<LookUp>
{
    public LookUp () { }

    public virtual string DisplayName { get; set; }
    public virtual LookUpType Type { get; set; }
}

dropdowns are used for LookUps and other objects so my custom model binder for Class/Create would do something like:

LookUp ClassType = LookUp.Load(long.Parse(bindingContext.ValueProvider.GetValue("ClassType").AttemptedValue))

I don’t see how something like this can be dealt with using the DefaultModelBinder.

  • 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-13T02:43:05+00:00Added an answer on June 13, 2026 at 2:43 am

    So I’ve come up with a working (for now) solution. It’s not very generic at the moment but it works for my purposes.

    public class ModelBinder : DefaultModelBinder
    {
        public override object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var ModelType = bindingContext.ModelType;
            var Instance = Activator.CreateInstance(ModelType);
            var Form = bindingContext.ValueProvider;
    
            foreach (var Property in ModelType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance))
            {
                Type PropertyType = Property.PropertyType;
    
                // I'm using an ORM so this checks whether or not the property is a
                // reference to another object
                if (!(PropertyType.GetGenericArguments().Count() > 0 ))
                {
                    // This is the not so generic part.  It really just checks whether or 
                    // not it is a custom object.  Also the .Load() method is specific
                    // to the ORM
                    if (PropertyType.FullName.StartsWith("Objects.Models"))
                    {
                        var Load = PropertyType.GetMethod("Load", BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static, null, new Type[] { typeof(long) }, null);
                        var Value = Load.Invoke(new object(), new object[] { long.Parse(Form.GetValue(Property.Name + ".ID").AttemptedValue) });
                        Property.SetValue(Instance, Value, null);
                    }
                    // checkboxes are weird and require a special case
                    else if (PropertyType.Equals(typeof(bool)))
                    {
                        if (Form.GetValue(Property.Name) == null)
                        {
                            Property.SetValue(Instance, false, null);
                        }
                        else if (Form.GetValue(Property.Name).Equals("on"))
                        {
                            Property.SetValue(Instance, true, null);
                        }
                    }
                    else
                    {
                        Property.SetValue(Instance, Convert.ChangeType(bindingContext.ValueProvider.GetValue(Property.Name).AttemptedValue, PropertyType), null);
                    }
                }
            }
    
            return Instance;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.