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

  • Home
  • SEARCH
  • 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 7559029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:31:48+00:00 2026-05-30T12:31:48+00:00

I have a type in an assembly which isn’t referenced by the core library

  • 0

I have a type in an assembly which isn’t referenced by the core library but is referenced from the web application. e.g.

namespace MyApp.Models {
    public class LatestPosts {
        public int NumPosts { get; set; }
    }
}

Now i have the following code in the core library:

[HttpPost, ValidateAntiForgeryToken]
public ActionResult NewWidget(FormCollection collection) {
    var activator = Activator.CreateInstance("AssemblyName", "MyApp.Models.LatestPosts");
    var latestPosts = activator.Unwrap();

    // Try and update the model
    TryUpdateModel(latestPosts);
}

The code is quite self explanatory but latestPosts.NumPosts property never updates even though the value exists in the form collection.

I’d appreciate it if someone could help explain why this does not work and whether there is an alternative method.

Thanks

  • 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-30T12:31:50+00:00Added an answer on May 30, 2026 at 12:31 pm

    Your problem has nothing to do with the fact that the type is in another assembly or that you are dynamically creating it with Activator.Create. The following code illustrates the issue in a much simplified way:

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult NewWidget(FormCollection collection) 
    {
        // notice the type of the latestPosts variable -> object
        object latestPosts = new MyApp.Models.LatestPosts();
    
        TryUpdateModel(latestPosts);
    
        // latestPosts.NumPosts = 0 at this stage no matter whether you had a parameter
        // called NumPosts in your request with a different value or not
        ...
    }
    

    The problem stems from the fact that Controller.TryUpdateModel<TModel> uses typeof(TModel) instead of model.GetType() to determine the model type as explained in this connect issue (which is closed with the reason: by design).

    The workaround is to roll your custom TryUpdateModel method which will behave as you would expect:

    protected internal bool MyTryUpdateModel<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 => new BindAttribute().IsPropertyAllowed(propertyName);
        IModelBinder binder = Binders.GetBinder(typeof(TModel));
    
        ModelBindingContext bindingContext = new ModelBindingContext()
        {
            // in the original method you have:
            // ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(TModel)),
            ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()),
            ModelName = prefix,
            ModelState = ModelState,
            PropertyFilter = propertyFilter,
            ValueProvider = valueProvider
        };
        binder.BindModel(ControllerContext, bindingContext);
        return ModelState.IsValid;
    }
    

    and then:

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult NewWidget(FormCollection collection) 
    {
        object latestPosts = new MyApp.Models.LatestPosts();
    
        MyTryUpdateModel(latestPosts, null, null, null, ValueProvider);
    
        // latestPosts.NumPosts will be correctly bound now
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# 4.0 application that is referencing a type library from a
I have a .NET assembly which has defined a type T at compile time,
Consider i have an assembly(class library dll) which i have loaded using the following
I have a struct which has several arrays within it. The arrays have type
I have been writing an assembly to simplify much of my POCO type generation.
I have an Assembly(A) which defines a Managed class which has a public constructor
I have a ASP.net application that is referencing a external assembly that I need
I have a set of assembly function which I want to use in C
I have a reference to a type, for which I would like to retrieve
OK, I have the following bit of code: Assembly assembly = Assembly.LoadFile(W:\\AssemblyFoo.dll); foreach (Type

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.