Best place to host SelectListItem generators in an ASP.NET MVC application

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!

+3
source share
2 answers

You can prepare your models for sets of values ​​for all drop-down lists. Then, in your opinion, you will simply use the properties of the model. You are right in thinking that this is not the responsibility of the dispatcher.

, . . , , , .

, , , . , , , . , .

+2

HtmlHelper , . . http://msdn.microsoft.com/en-us/library/bb383977.aspx. , . Web.config, .

<pages>
    <namespaces>
        <add namespace="MyApplication.Extensions"/>
    </namespaces>
</pages>
+1

Source: https://habr.com/ru/post/1717086/


All Articles