Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
buntykawale Avatar asked May 20 '09 06:05

buntykawale


3 Answers

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.

like image 134
Todd Smith Avatar answered Oct 12 '22 02:10

Todd Smith


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

like image 31
Arnis Lapsa Avatar answered Oct 12 '22 03:10

Arnis Lapsa


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.

like image 29
Carl Hörberg Avatar answered Oct 12 '22 03:10

Carl Hörberg