views:

416

answers:

4

Hi folks, 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?

+2  A: 

I haven't tried this yet, but read somewhere that resources works the same way they worked in web forms.

Arnis L.
I tried including namespace in View and then accessing it using Resources but its not working..
buntykawale
+3  A: 

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.

Todd Smith
+2  A: 

Use <%= Resources.Strings.MyCustomString %>. I don't understand what Todd mean by "it doesn't support localization", it's exactly what it does..

Carl Hörberg
A: 

Hi guys, Thanks for your responses.This helped me resolve the issue.

buntykawale