MVC3 ModelBinder for DynamicObject

I want to see if there is a project sample, tutorial, contrib tab or something like that that details implementation of custom ModelBinder for MVC3 to support objects inheriting from DynamicObject.

I have a domain object that has a dynamic number of properties defined by the database, and they can change at runtime. To simplify the use of the object, I made the inheritance of the class inherited from DynamicObject and passed the model to the view using the [dynamic] keyword.

All dynamic properties of an object are in the property of a collection of an object called Attributes. I would like to create a series of editor templates to model the model, ideally, so I need to make a line call Html.EditorForModel (), and it will dynamically create a user interface.

The problem is that I was not very lucky to find an implementation of ModelBinder capable of checking DynamicObject and aligning the user interface (I think the right term for this?).

I found the IDictionary ModelBinder project made in one of MVCConf videos made by Roberto Hernandez (@hernandezrobert) on MVC3 Extensability (source at http://mvcextensibility.codeplex.com/ ), but I was unable to adapt it to my goals. I was wondering if anyone was trying to create a model binder that can do what I am describing? or maybe point me in the right direction?

0
source share
2 answers

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.

+1
source

ModelBinders do not help generate a view, they help to match raw parameters from various web sources (form, querystring, etc.) with the input parameters expected by your action (in particular, if your input parameters are a class of some kind, not primitives).

What you are looking for is an example of how to create presentation templates that I have not seen for dynamics. The best resource for creating a regular presentation template so far I've seen Brad Wilson's blog . If you have a tool (which sounds like you), figuring out what properties the object should display along w / metadata on how to display them (for example, textarea versus input type = text, etc.), then you should to be able to just follow Brad.

+3
source

Source: https://habr.com/ru/post/1393803/


All Articles