asp.net-mvcfileresources

Can I access Resources file from a View in MVC?


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?


Solution

  • 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.