I want to populate dropdownlist with values stored in a resource file. What's the best approach for this?
I can create a SelectList and push it in Model in which case dropdown would be populated automatically. But can I access resource file from a View ? If yes, should I?
All resource strings get compiled into a class which you can reference in your views. Example:
<%= Resources.Strings.MyCustomString %>
I believe the following is automatically added to your web.config so you can drop the Resources..
<namespaces>
<add namespace="Resources">
</namespaces>
However, this will not support localization. For that you'll want to use a helper method.
If you're trying to populate a list you'll need to create a helper class that can iterate through the Strings class and extract the appropriate values or encode your selections in a comma delimited list and parse/split that before feeding it to your dropdownlist's selectionlist.
I haven't tried this yet, but read somewhere that resources works the same way they worked in web forms.
Use the following:
<%= Resources.Strings.MyCustomString %>
I don't understand what Todd mean by "it doesn't support localization", it's exactly what it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With