Tuesday 15 June 2010

asp.net - Best practices to fill view models with select lists and options for views' reusability in ASP MVC -



asp.net - Best practices to fill view models with select lists and options for views' reusability in ASP MVC -

i'm using view models on models. let's have edit form entity. let's form has field selected drop downwards list. best way fill list used generating drop downwards list? should done in action in controller or maybe in constructor of view model or somewhere else?

my next uncertainty view models "configuring". i'm trying reuse views when have edit form should possible utilize in add together , update scenarios. submit button should execute 2 different actions. okay parametrize view adding view model controller , action names?

finally when have view models storing info - "configuration" quite vast. there pattern logic out of controller? i'm using kind of builders, i'm not sure whether it's enough.

first of excuse me if won't exhaustive topic pretty vast. please read cum grano salis, nil rule written in stone things must adapted case case each specific scenario.

what best way fill list used generating drop downwards list? should done in action in controller or maybe in constructor of view model or somewhere else?

controller method(s). keep model (underlying model or view model) as simple possible (ideally simply entity) because model isn't place logic (even if can relaxed underlying model because in domain objects logic , info tied together).

moreover list items may added/removed according many factors , span logic across controllers , model.

imagine have next requirement: "design view user can pick film list, list populated using past history pick best candidates; provide dropdown filter movies category". there lot of logic here, search in history, kind of smart algorithm pick best candidates , function prepare list. of course of study if there isn't drama film wouldn't set item in list. if set something in model you'll have set everything there, leaving controller stupid entity simply create model instance , pick right view. little, because controller methods easy reused between views model classes pretty more specific.

moreover here move list out of model. maintain model focused on real objects, user see , selects (so, if need it, don't worry json serialize them). if have model classes logic , others without (because they're json results, example) you'll find search logic both in model , controller. pretty confusing. available options (like film categories in previous example) view specific details maintain in viewbag.

is okay parametrize view adding view model controller , action names?

yes it's ok per se should inquire if it's not improve extract partialview, they'll embedded in outer view , it'll automatic; assuming have typical pattern:

actionresult edit() { } [httppost] edit(model data) { }

...view models storing data... there pattern logic out of controller?

i can't give here decent reply because depends on case. if view logic complex may need introduce lean layer encapsulate these stuff (controller interact them , set things won't aware of logic specific details).

storing info (model <-> view model) somehow easier, (often? always?) oom automapper solve of problems (without issues have orms). mapping done convention , if follow won't need write more few lines of code (and complicated cases can configured).

asp.net asp.net-mvc asp.net-mvc-4

No comments:

Post a Comment