title: ASP.NET MVC Issue with Using Reflection Created Objects with the Default Model Binder
body: <p>I am having a weird issue in ASP.NET MVC with objects not being updated with <strong>UpdateModel</strong> when passed a <strong>formCollection</strong>. <strong>UpdateModel</strong> does not appear to be working properly when the object being updated is created through reflection.</p>
  
  <p>Scenario: I have an application which has approximately 50 lookup tables--each of which includes exactly the same schema including typical fields like id, title, description, isactive, and createdon. Rather than build 50 views, I wanted to have a single view which could display the data from all of the lookup tables. I created an Interface called IReferenceEntity and implemented it in each of the POCOs representing my lookup tables.</p>
  
  <p>Using this interface, I am able to easily populate a view with a record from the lookup table. (I pass the items to the view via the following.)</p>
  
  <pre><code>System.Web.Mvc.ViewPage&lt;MyNamespece.IReferenceEntity&gt; 
  </code></pre>
  
  <p>From the database to the view, every thing works perfectly.</p>
  
  <p>However, when I attempt to update the model on post, I am running into some problems.</p>
  
  <p>If I explicitly declare an object reference like the following, every thing works perfectly and the values of my object are updated with the values from my form. Hence, I can then update the database.</p>
  
  <pre><code>AccountStatus a = new AccountStatus();
  
  UpdateModel(a, formCollection.ToValueProvider());
  </code></pre>
  
  <p>Unfortunately, hard coding the object type would completely defeat the reason for using an interface. </p>
  
  <p>(A primary objective of the application is to be able to dynamically add new tables such as lookup tables without having to do anything "special". This is accomplished by reflecting on the loaded assemblies and locating any classes which implement a specific interface or base class) </p>
  
  <p>My strategy is to determine the concrete type of the object at postback and then create an instance of the type through reflection. (The mechanism I use to determine type is somewhat primitive. I include it as a hidden field within the form. Better ideas are welcome.)</p>
  
  <p>When I create an instance of the object using reflection through any of the following methods, none of the objects are being updated by UpdateModel.</p>
  
  <pre><code>Type t = {Magically Determined Type}
  
  object b = Activator.CreatorInstance(t);
  
  UpdateModel(b, formCollection.ToValueProvider());
  
  
  Type t = {Magically Determined Type}
  
  var c = Activator.CreatorInstance(t);
  
  UpdateModel(c, formCollection.ToValueProvider());
  
  
  Type t = {Magically Determined Type}
  
  IReferenceEntity d = Activator.CreatorInstance(t);
  
  UpdateModel(d, formCollection.ToValueProvider());
  </code></pre>
  
  <p>Note: I have verified that the objects which are being created through relection are all of the proper type.</p>
  
  <p>Does anyone have any idea why this might be happening? I am somewhat stumped.</p>
  
  <p>If I was really "hard up", I could create factory object which would many instantiate any one of these reference entity/lookup objects. However, this would break the application's ability to allow for new lookup tables to be added and discovered transparently and is just not quite as clean.</p>
  
  <p>Also, I could try deriving from an actual ReferenceEntity base class as opposed to an interface, but I am doubtful whether this would make any difference. The issue appears to be with using reflection created objects in the modelbinder.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Anthony</p>