In my first project in ASP.NET MVC, I come across several situations where I have drop-down lists that are limited in their purpose to a single view. A good example is that I have a registration form where the user must indicate their gender and date of birth. After entering anywhere in the application, it has not changed.
For gender I create gender groups and for date of birth, I create separate pairs (months, days and years) for their respective drop-down lists. Now I have methods that generate these values in the controller that returns this view (in the constructor). I just can't help but think that this causes a bit of code smell because not every method in the controller always returns a view (possibly due to REST-ful calls, etc.). I don’t think that generating them every time is an approach to be taken.
What methods do people use to mediate? I am thinking of some class with static methods that can be called directly from the assignment (<% = Html.Dropdown ("myitem", GeneratorMethod ())%>). The only problem that I can avoid because of this is the ability to figure out how to re-select an item if the view is returned again (due to a validation error, for example).
Any feedback would be appreciated!
source
share