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
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:The problem stems from the fact that
Controller.TryUpdateModel<TModel>
usestypeof(TModel)
instead ofmodel.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:and then: