asp.net mvc - How do you wire lists/collections on view models using IoC, DI, and MVC? -


i'm working mvc project, trying implement ioc , di. both these new me, apologies in advance if i'm going down wrong road, or if question not worded clearly.

suppose have view needs display address form, 1 of elements being drop down list of states. (i realize address outside context of else not likely, simplicity of example, assume case here.)

suppose view model:

public class addresslist {     public string address1 { get; set; }     public string address2 { get; set; }     public string city { get; set; }     public list<string> states { get; set; }     public string zip { get; set; } } 

whose responsibility populate states list?

should controller this, having had service injected getting states? if so, same injection , population logic go in each of controllers needing populate address? or should go somewhere else?

at layer view model belong? if view model specific given view in app, shouldn't remain in app, , therefore not used pass service populating states?

i've searched few hours trying find example of doing this, , finding little help. i'm wondering if wrong approach begin with. if so, i'm open going differently.

i'm working asp.net mvc2, unity, , ef4, examples using great. however, examples steer me in right direction.

thanks in advance.

edit: sorry, think may have stumbled on i'm looking -- model binding:

http://www.dominicpettifer.co.uk/blog/39/dependency-injection-in-asp-net-mvc-2---part-2--modelbinders-viewmodels

is need? work unity (i see examples here use castle windsor)?

like other application boundary data structures, viewmodels best modeled data structures bit of behavior. controllers' responsibility load data , populate viewmodels.

thus, should inject necessary services controller , use services populate viewmodel.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -