views:

85

answers:

2

I was thinking of creating a UtilityController that only contains actions which return json because I might have several forms with a province/state dropdown and a country dropdown. These dropdowns are filtered by selection with ajax...only show prov/state in selected country.

What do you think about this idea...is it good...bad...neither?

Thanks

+1  A: 

I follow a similar pattern in one of my web applications. I default the page to loading US states and have USA pre-selected in the dropdown since a large majority of visitors are from there, but then use JSON to load sates if they switch.

Parrots
+1  A: 

I would not recommend having a utility controller or any kind of utility classes. In my opinion that is bad practice as it will easily become a dumping ground for code that you don't know where it belongs. This violates the S.O.L.I.D principals as you do not separate your concerns. It is better to look at what actions you want and find the appropriate controller for each one of them.

Mattias Jakobsson
That is kind of how it felt to me. So, since the country/province dropdowns will be utilized for mutiple controllers...are you suggesting making an action on each controller...say GetProvincesByCountryId(int countryId), that needs that functionality? Then all the GetProvincesByCountryId actions would just use the same code to generate the result?Is it considered reasonable to have the same method signature on mutiple controllers?
Cedar Teeth
You don't need to have it on the controller that handles the current request. You can pick any controller that handles that kind of thing and call that action from all views that need that data. For instance, if you have something like a ProvinceController you would put the "GetProvincesByCountryId" method on that controller. This is not violating anything as the json request is a seperate request that has nothing to do with the initial request for the view. I would not recomend having the same signature on multiple controllers if they are handling the same thing.
Mattias Jakobsson
Oh, ok...instead of putting the action at the bottom of say the registration controller have a Provinces controller with related methods. Oddly that never occured to me and makes a lot of sense. Awesome, thanks!
Cedar Teeth