Dai
Dai

Reputation: 155055

ASP.NET MVC - Model-binding for different views at runtime

My latest project involves actions returning different views at runtime (simply put, the application has a number of different customers as users, and they want their own custom data entry form designs - these forms all share the same model object-graph, but their display of the model's data is radically different to each other)...

For example, a "Case" entity has multiple People associated with it. Some forms only display a single Person's fields, other forms support multiple Persons, and another doesn't contain Person information at all. Furthermore, the forms have differing levels of fields - many forms lack many fields that others have.

Because of this radically different behaviour, I believe I'll need a different model binder for each view design.

Can StackOverflow recommend a course of action to take in this case, or an alternative solution that keeps the code simple.

Upvotes: 3

Views: 839

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

If all your view models derive from a common abstract base view model you could have your POST action take this common view model as parameter and then write a custom model binder which will instantiate and bind the correct instance assuming each view sends an additional parameter containing the concrete type.

Here's an example of how this could be done.

Upvotes: 2

Related Questions