I managed to solve my situation by setting a normal class to serve as a model for my DanamicObject, and so that my properties are saved like this
IList<DynamicProperty> DynamicProperties { get; set; }
I created a custom view for DynamicObject, and in this view I called one of the helpers to display the DynamicProperties property. This allows MVC to go through the collection and display each property. Then I have a view for DynamicProperty, which I use to render the property as needed. Key - You must also display a hidden field containing the primary key of this particular attribute. The ModelBinder in MVC3 works much better here than in MVc2, so it will display the index of the array as part of the field name so that the primary key and value of each property pair correctly in submit. You probably want to create a ViewModel specifically for the presented data, I had problems when I tried to use the same model class that was used in the information / edit views, because I only displayed a subset of the fields, so they were missing when I was snapped to that same model class in reverse gear.
You can handle saving, but you would do it, but there are a few considerations for this type of object. Since the number of attributes is dynamic, it cannot be guaranteed that as many fields will be sent as was originally submitted. The user can enter their own or worse field additions for a property that you may have explicitly excluded. AntiForgeryToken would prevent such submissions from happening outside of your domain, but with the ease and popularity of DOM manipulation provided by libraries like jQuery, cross-site postbacks are not the only problem, and I donβt know if AntiForgeryToken will be responsible for this. but I doubt it.
This approach turned out to be simpler than trying to inherit from DynamicObject, implement GetDynamicMemberNames and make your own ModelBinder to use the wrapper around it.
However, I created a custom provider ModelMetaData and Validation to handle these aspects, as the properties were not strongly typed, so MVC did not have any annotations to use.
source share