views:

178

answers:

2

In my first venture into ASP.NET MVC, I am coming across a few situations where I have dropdown lists that are limited in their purpose to a single view. A good example is that I have a signup form where a user has to put in their gender and birthdate. Once entered, nowhere in the application is it changed.

For the gender, I build out the genders and for the birthdate, I build out the individual peices (months, days, and years) for their respective dropdowns. Now, I have the methods that generate these values in the controller that returns this view (in the constructor). I just can't help but to think that there is a bit of a code smell coming from doing it this way, though, since not every method in the controller will always return the view (possibly because of REST-ful calls, etc.). I don't think that generating these every time is the approach to take.

What methods to people use to mediate this? I am thinking some sort of a class with static methods that can be called directly from the assignment (<%= Html.Dropdown("myitem", GeneratorMethod()) %>). The only problem that I can forsee out of doing it this way is being able to figure out how to re-select an item in the event the view is returned again (due to a validation error, for example).

Any feedback would be appreciated!

+1  A: 

I usualy extend the HtmlHelper class with the methods I want to be reusable across multiple views. For further Information about extension methods see http://msdn.microsoft.com/en-us/library/bb383977.aspx. I hold all my classes which contain extension methods in the same namespace. Then I define the classes in Web.config to be available in all views.

<pages>
    <namespaces>
        <add namespace="MyApplication.Extensions"/>
    </namespaces>
</pages>
Mato
I am definitely considering wrapping some of the functionality up in HtmlHelper extensions. Specifically, this is something that I am looking at for the dropdown combo generation for dates, since I can see the reuse and setup that goes into building these. I will definitely import the namespace in the Web.config, since it helps keep that responsibility out of the View, where I am expecting to hand off to a designer in the future.
joseph.ferris
+2  A: 

You can have your models prepare the value sets for all dropdown lists. Then in your view you will just use on of the model properties. You're right thinking it does not belong to controller responsibility.

You can create a static class to return values sets for all choice situations your application needs to support. Your models could be calling these methods in the constructor. Of course, you can call them directly in view helpers, but it is a good approach to try to minimize outside view communication.

To reselect, you will need to catch a submitted value, set it to one of the model properties, then return this model. Whether a built-in helper accepts a pre-selected value I do not exactly remember, I stopped using built-in helpers quite quickly. I suppose they should be able to.

Developer Art
This makes a lot of sense. I had toyed with the idea of adding these values into my model (I am not using the built-in model, but an external repository implementation) and this leads me to believe that this will probably be the best approach. My repository implementations include entity caching, so after the first cold call, it will be cached. I am still unsure of the reselection process, but I am sure that I can figure it out now that I am pointing in the right direction. Thanks!
joseph.ferris