Question

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

Was it helpful?

Solution

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
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top