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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:06:37+00:00 2026-05-15T19:06:37+00:00

I have forms posting data from instances of a particular abstract class: public abstract

  • 0

I have forms posting data from instances of a particular abstract class:

public abstract class IRestriction
{
    public string Name {get; set;}
    public abstract IModelBinder GetBinder();
}

The concrete type and PartialView are determined at runtime:

IRestriction restriction = (IRestriction)Activator.CreateInstance(Type.GetType(restriction.restriction_class));

The appropriate partial view is then rendered correctly.

When the form is sent back the type is inferred correctly and activated the same way.

However, I haven’t been able to get UpdateModel to bind to the concrete implementation.

How do I get the Model to bind to the concrete type instead of the interface?

Things I’ve tried:

I’ve set the ModelBinderAttribute on the concrete class but it is being ignored.

[ModelBinder(typeof(MyCustomModelBinder))]
public class ConcreteRestriction : IRestriction

I’ve cleared all the ModelBinders and added only the binder from the interface.

Binders.Clear();
Binders.Add(item.GetType(), item.GetBinder());

None of these is working.

Whats the best way to accomplish what I’m trying to do?

Is ModelBinderAttribute being ignored in error?

** ———————-UPDATE———————-**

Here is a solution for anyone else struggling with the same issue who happens to run across this.

The following class inherits Controller. Inherit it and call UpdateModelDynamic()/TryUpdateModelDynamic()

public class DynamicTypeController : Controller
{
    internal static bool IsPropertyAllowed(string propertyName, string[] includeProperties, string[] excludeProperties)
    {
        // We allow a property to be bound if its both in the include list AND not in the exclude list.
        // An empty include list implies all properties are allowed.
        // An empty exclude list implies no properties are disallowed.
        bool includeProperty = (includeProperties == null) || (includeProperties.Length == 0) || includeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase);
        bool excludeProperty = (excludeProperties != null) && excludeProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase);
        return includeProperty && !excludeProperty;
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model) where TModel : class
    {
        return TryUpdateModelDynamic(model, null, null, null, ValueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix) where TModel : class
    {
        return TryUpdateModelDynamic(model, prefix, null, null, ValueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string[] includeProperties) where TModel : class
    {
        return TryUpdateModelDynamic(model, null, includeProperties, null, ValueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties) where TModel : class
    {
        return TryUpdateModelDynamic(model, prefix, includeProperties, null, ValueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class
    {
        return TryUpdateModelDynamic(model, prefix, includeProperties, excludeProperties, ValueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, IValueProvider valueProvider) where TModel : class
    {
        return TryUpdateModelDynamic(model, null, null, null, valueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix, IValueProvider valueProvider) where TModel : class
    {
        return TryUpdateModelDynamic(model, prefix, null, null, valueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string[] includeProperties, IValueProvider valueProvider) where TModel : class
    {
        return TryUpdateModelDynamic(model, null, includeProperties, null, valueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, IValueProvider valueProvider) where TModel : class
    {
        return TryUpdateModelDynamic(model, prefix, includeProperties, null, valueProvider);
    }

    protected internal bool TryUpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel : class
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }
        if (valueProvider == null)
        {
            throw new ArgumentNullException("valueProvider");
        }

        Predicate<string> propertyFilter = propertyName => IsPropertyAllowed(propertyName, includeProperties, excludeProperties);
        IModelBinder binder = Binders.GetBinder(model.GetType());

        ModelBindingContext bindingContext = new ModelBindingContext()
        {
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()),
            ModelName = prefix,
            ModelState = ModelState,
            PropertyFilter = propertyFilter,
            ValueProvider = valueProvider
        };
        binder.BindModel(ControllerContext, bindingContext);
        return ModelState.IsValid;
    }


    protected internal void UpdateModelDynamic<TModel>(TModel model) where TModel : class
    {
        UpdateModelDynamic(model, null, null, null, ValueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix) where TModel : class
    {
        UpdateModelDynamic(model, prefix, null, null, ValueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string[] includeProperties) where TModel : class
    {
        UpdateModelDynamic(model, null, includeProperties, null, ValueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties) where TModel : class
    {
        UpdateModelDynamic(model, prefix, includeProperties, null, ValueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) where TModel : class
    {
        UpdateModelDynamic(model, prefix, includeProperties, excludeProperties, ValueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, IValueProvider valueProvider) where TModel : class
    {
        UpdateModelDynamic(model, null, null, null, valueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix, IValueProvider valueProvider) where TModel : class
    {
        UpdateModelDynamic(model, prefix, null, null, valueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string[] includeProperties, IValueProvider valueProvider) where TModel : class
    {
        UpdateModelDynamic(model, null, includeProperties, null, valueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, IValueProvider valueProvider) where TModel : class
    {
        UpdateModelDynamic(model, prefix, includeProperties, null, valueProvider);
    }

    protected internal void UpdateModelDynamic<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties, IValueProvider valueProvider) where TModel : class
    {
        bool success = TryUpdateModelDynamic(model, prefix, includeProperties, excludeProperties, valueProvider);
        if (!success)
        {
            string message = String.Format("The model of type '{0}' could not be updated.", model.GetType().FullName);
            throw new InvalidOperationException(message);
        }
    }

}
  • 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-15T19:06:38+00:00Added an answer on May 15, 2026 at 7:06 pm

    I think it’s a bug, but the ASP.NET MVC team disagrees. Model binding looks at the static, compile-time type of the model. I don’t like it, but that’s how it is.

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

Sidebar

Related Questions

No related questions found

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.