views:

32

answers:

1

A generally accepted way to pass all data to a view is to have a single data model with references to both your domain model and additional data for things like drop down lists (DDL).

However, partial views (view templates too) receive only a portion of the main model, not able to access the root of the Model sent to the original view. So if your DDL lists are not static, how do the partial views get the data?

Is there a way using [Data Annotations] to reference a method which could return the possible values of a field, then use this in the partial view's DDL? Where would this method exist, in the repository?

Links or C# code examples would be very helpful.

+1  A: 

There is no built in Data Annotations attribute that could do what you ask.

You could create your own attribute that contains a reference to a Type and the name of a static method that you can then invoke via reflection from your partial view.

Where you would place such a method depends on what you are doing, though I still think that gathering all the inputs in your controller would be better. You can always set extra items in the ViewData collection and pass those into your partial views.

marcind
I would think that the static method would either be in the repository or a service that accesses the repository. I would assume that a view isn't allowed to access this service directly. However, I wonder if helper methods could access data in a service layer directly without violating the view's concerns? If so, this is a great answer: Use helper methods that access a service layer directly to create custom DDLs, like Html.CityDropDownList().
Dr. Zim