Christer Kolterjahn
Christer Kolterjahn

Reputation: 121

Referencing resource files from a razor view

I use ASP.NET Internationalization from Code52 at http://code52.org/aspnet-internationalization/ which uses (language) resource files.

From my controller I reference the string I want like this

ViewBag.Message = Language.Index_Title;

Is it possible to reference the values directly in the (razor) view? Something like this: @Resources.Language.Index_Title;

Upvotes: 12

Views: 11928

Answers (3)

Arlex Velásquez
Arlex Velásquez

Reputation: 51

Modify your Resource file in properties in Custom Tool you have to add the value "PublicResXFileCodeGenerator" as well as in your view you can use the resource file.

Upvotes: 5

Romias
Romias

Reputation: 14133

In your web.config you have the "Pages" section... there you need to add your Resource namespace... that way you don't need to declare it in every page using "using".

<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
    <add namespace="Your.Resources.Namespace" />  <!--ADD THIS LINE-->
  </namespaces>
</pages>

Then, assuming your resource file is called "Language", in your Razor view you use:

@Language.Index_Title

Upvotes: 11

hopper
hopper

Reputation: 4338

Try @ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey) . Hope this helps

Upvotes: 3

Related Questions