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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:34:07+00:00 2026-06-11T20:34:07+00:00

I have and issue binding to my model with an action filter using reflection.

  • 0

I have and issue binding to my model with an action filter using reflection. The problem im having is I cannot access the sub class to set the value to a property –> FilePath (string).

Here are the goals I wanted to achive.

1) Upload images using an action filter so I dont reference the HttpContext in my controller action.

2) Force consistancy as all of my models using this method will have to inherit from a generic base abstract class BaseModel<T>.

3) Automatically bind file paths to the model entity

The generic type entity of BaseModel<T> is implementing an interface (IUpload) which will force the use of the property I want to be able to set (FilePath) for further consistancy and generics.

Here is my binding attempt within my action filter…

   private void MyCustomBindVoid(ActionExecutingContext filterContext, string filePath)
    {
        foreach (object x in filterContext.ActionParameters.Values)
        {
            Type baseType = GetGenericTypeBase(x.GetType(), typeof(BaseModel<>));

            if (baseType == null)
            {
                throw new NullReferenceException("All view models must inherit from the BaseModel abstract class.");
            }

            foreach (PropertyInfo prop in baseType.GetProperties())
            {
                Type entity = Type.GetType(prop.PropertyType.AssemblyQualifiedName, true, false);

                if (entity != null)
                {
                    foreach (Type i in entity.FindInterfaces((type, criteria) => true, null))
                    {
                        if (i == typeof(IUpload))
                        {
                            foreach (MemberInfo info in i.GetMembers())
                            {
                                PropertyInfo entityInfo = entity.GetProperty(info.Name);

                                if (entityInfo != null)
                                {
                                    entityInfo.SetValue("I_CANNOT_FIGURE_HOW_TO_GET_THE_OBJECT_TO_BIND_HERE", filePath, null);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private Type GetGenericTypeBase(Type type, Type genericType)
    {
        while (type != typeof(object))
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == genericType)
            {
                return type;
            }

            type = type.BaseType;
        }

        return null;
    }

I know its not ideal model binding in the action filter but I have run out of ideas of passing the file path to the model.

This does seem to work and I’ve acomplished this by putting a FilePath (string) property onto the model which is inheriting from BaseModel<T> which can be accessed by the following. As the “x” object is available as a concreate instance.

foreach (MemberInfo info in i.GetMembers())
{
   PropertyInfo baseInfo = baseType.GetProperty(info.Name);

   if (baseInfo != null)
   {
      baseInfo.SetValue(x, filePath, null);
      return;
    }
 }

This again will lead to writing the same code in my controller action as i will have to manually set the FilePath property on the BaseModel<T> to the FilePath property of the entity <T>.

Any help will be very much appreciated (Even if it means a drastic rethink)

  • 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-11T20:34:08+00:00Added an answer on June 11, 2026 at 8:34 pm

    Found what I was looking for courtesy of http://codecallback.com/

    The full explaination can be found below.

    http://codecallback.com/forum/3/thread/4/asp-net-mvc-3-model-binding-to-sub-class-with-action-filter-using-reflection

    All I need to do was to create an instance of the sub class like so.

    PropertyInfo entityInfo = entity.GetProperty(info.Name);
    if (entityInfo != null)
    {
      object y = prop.GetValue(x, null); /* THIS IS THE FIX */
      entityInfo.SetValue(y, filePath, null);
      return;
    }
    

    Doing this will allow me to decorate my action with a [UploadFileAttribute(param, param)] and let all the file upload, path generation and binding to the property of the generic entity model get taken care of so all I had to do was call MyRepository.Create(Model.Entity);

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

Sidebar

Related Questions

The problem we are having is that we cannot get binding to work in
I have an issue using the protectedObservable custom binding below found at this link.
I have an issue with model binding a checkbox from a form to an
I have an updating issue with Binding in my ComboBox. I have created a
I have a WPF sorting/binding issue. (Disclaimer: I am very new to WPF and
I'm having an issue bending my head around this one. I have a table
This seems like the model binding is causing me issues. Essentially I have a
I have a ViewModel class to encapsulate Personal and Business models. My problem is
I have a TreeView whose ItemsSource is set to a Model I have. 3
I am having trouble binding my View Model to my View . I am

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.