开发者

Returning ad-hoc view models from ASP.NET MVC2 Controllers

开发者 https://www.devze.com 2023-01-17 13:11 出处:网络
I\'m porting an existing system to ASP.NET MVC2.In the current legacy app, the user can select from dozens of available fields to customize CRUD forms for different entities in the domain model, simil

I'm porting an existing system to ASP.NET MVC2. In the current legacy app, the user can select from dozens of available fields to customize CRUD forms for different entities in the domain model, similar to the way ERP systems allow customization of core modules.

My question: I'm looking for a good pattern or example for this kind of behavior in ASP.NET MVC2. It seems to me it's rather like creating a ViewModel dynamically based upon user choices, or perhaps the right approach is data-driven view pages that aren't strongly-typed where I can reflect over the results client-side to determine field headings or som开发者_如何学Cething -- if that makes sense :). Or maybe I can drive AutoMapper or similar dynamically @ runtime based on user choices?

The underlying domain model is EF4-based and I'm using a simple Repository pattern @ present for the ViewModel.

TIA for any input! Michael


If I didn't find anything else that matched the needs and went on to do it custom, I would:

  • Use the ViewModel with all the fields / not just the ones the user picked.
  • Pass both the ViewModel and the view configuration to the view
  • Call some html helper that for each item in the configuration adds a field with the corresponding property in the model
  • The configuration could be passed as either part of a containing ViewModel or in a separate entry in ViewData
  • Depending on what you need, building/passing the view configuration could be put in an Action Filter. Alternatively the helper could pull it directly.

A different approach is if you need completely custom fields. I mean user defined fields. If that's the scenario, that's not typed at the controller level already, so I'd pass the list of fields/values to the view. The view can do a foreach on those adding the fields. Again that could be moved to a HtmlHelper.

0

精彩评论

暂无评论...
验证码 换一张
取 消