title: Dynamic Model (non-class) Metadata Provider in MVC
body: <p>We are developing an application where the end-user schema is dynamic (we have a good business case for this - it is not something that can be handled easily by a static model).</p>
  
  <p>I have used the .NET DynamicObject class to allow these dynamic schema objects to be addressed easily from code, and expected this to just work with the MVC model metadata. However the MVC metadata support seems to be hamstrung in that it only deals with meta-data defined per type - not per object which will be the case here.</p>
  
  <p>Even when I dug down and tried implementing our own ModelMetadataProvider, it seems that the neccessary information is simply not passing in - the GetMetadataForProperty method is particularly problematic. Effectively I need to access the parent or container object for the property, but all that is passed in is the type. </p>
  
  <p>The above is called mainly from the FromStringExpression method in the ModelMetadata class. This method actually DOES have the container (at least in this case) but does not pass it through. This branch is executed when it finds the view data about the expression stored (cached?) in the ViewData. If that fails it falls back to looking it up via the ModelMetadata object - which ironically might work for me. Whats particularly irritating is that the FromStringExpression method is static, so I can't easily override its behavior.</p>
  
  <p>In desperation I have considered trying to traverse the modelAccessor expression, but this seems like a kludge at best and extremely fragile.</p>
  
  <p>I have searched extensively for a solution to this. Many point to Brad Wilson's talk (<a href="http://channel9.msdn.com/Series/mvcConf/mvcConf-2011-Brad-Wilson-Advanced-MVC-3">http://channel9.msdn.com/Series/mvcConf/mvcConf-2011-Brad-Wilson-Advanced-MVC-3</a>) on non-class models, however if you look at the actual code presented, you will see that it TOO is bound to the TYPE and not the object - in other words not terribly useful. Others have pointed to the <a href="http://fluentvalidation.codeplex.com/">http://fluentvalidation.codeplex.com/</a>, but that only seems to apply to the validation side, and I suspect suffers from the same problem (bound to type rather than object) as the above.</p>
  
  <p>For example, I may have a dictionary object that contains a series of field objects. This looks something like (very cut down/simplified example):</p>
  
  <pre><code>public class Entity : DynamicObject, ICustomTypeDescriptor
  {
      public Guid ID { get; set; }
      public Dictionary&lt;string, EntityProp&gt; Props { get; set; }
  
      ... DynamicObject and ICustomTypeDescriptor implementation to expose Props as dynamic properties against this Entity ...
  }
  
  public class EntityProp
  {
      public string Name { get; set; }
      public object Value { get; set; }
      public Type Type { get; set; }
      public bool IsRequired { get; set; }
  }
  </code></pre>
  
  <p>This might be passed to a view as its view-mode (or part of it), and in my view I'd like to use:</p>
  
  <pre><code>@Html.EditorForModel()
  </code></pre>
  
  <p>Has anyone found a way around this?</p>
  
  <p>I've identified two possible alternative approaches, but both have significant drawbacks:</p>
  
  <ul>
  <li>Abandon using the MVC ModelMetadata for this, and instead build a view-model that directly contains the necessary metadata, along with the templates needed to display these more complex view-model objects. Means however that I'm then having to treat these objects differently to 'normal' objects, defeating the purpose somewhat and increasing the amount of view templates we need to build. <strong>This is the approach I am leaning towards now - more or less abandoning integrating with the MVC ModelMetadata stuff</strong></li>
  <li>Generate a unique key for each templated property, and use this for the property name rather than the display name - that would allow the ModelMetadataProvider to find the metadata that related to the property without needing a reference to its parent. However this would lead to a fairly ugly situation when debugging, and again seems like a large scale kludge. <em>I have now tried a simplified version of this, and it seems to work, but does have some undesirable behavior, such as needing to use an meaningless property name if I want to explicitly bind to elements of the model.</em></li>
  <li>In the ModelMetadataProvider when returning a collection of ModelMetadata objects for contained properties, record the container in the ModelMetadataProvider that is associated with those returned properties. <em>I have tried this, but this returned collection of property metadata is ignored in this case, and the FromStringExpression method goes directly to the GetMetadataForProperty method instead.</em></li>
  </ul>