0

I'm trying to get a resource value for internazionalization based on the value of an foreach variable.

My resource project is named Resources, I reference this on my main project.

reference properties

When i want to use it. only use something like this: Resources.resourceKey

The Problem

Now I need to use this but resourceKey is in a variable on a loop

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => Resources.item.Rating)
        </td>
    </tr>
}

This obviusly not working, in Resources has no exists any key named item

My solution (Not working)

Create a Resource Manager and try to get a string

@{
    System.Reflection.Assembly resourcesAssembly;
    resourcesAssembly = System.Reflection.Assembly.Load("Resources");

    System.Resources.ResourceManager rm = new
       System.Resources.ResourceManager(resourcesAssembly.FullName,
       resourcesAssembly);
}

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => rm.GetString(item.Rating))
        </td>
    </tr>
}

What i'm doing wrong??

Really thnx for help

1 Answers1

0

First of all - using the reflection on View is bad style :) It seems like everything is OK. You are creating ResourceManager from another Assembly. If it is created correctly (view in debug) and you can see your resources in debug-watch - try to check your Culture and that your resource key is set for this culture. ResourceManager chooses the resource string depending on your thread's culture. By default it is you operating system culture.

From MSDN:

If no usable set of localized resources is found, the resource manager falls back on the default culture's resources. If the resource manager cannot load the default culture's resource set, the method throws a MissingManifestResourceException exception or, if the resource set is expected to reside in a satellite assembly, a MissingSatelliteAssemblyException exception. If the resource manager can load an appropriate resource set but cannot find a resource named name, the method returns null. MSDN article

So, if you don't see the exception - check if your resource-key is specified for choosen culture.

n.prokhorov
  • 930
  • 7
  • 13
  • No one of these exceptions are thrown. It looks like RM is generated right. In the line `Html.DisplayFor(modelItem => rm.GetString(item.Rating))` show next exception: An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Sorry for this question, maybe im doing something very very bad and obviusly, but im new on this technology =( Thnx – Enrique Barrero Ligero Mar 15 '16 at 10:01
  • Oooh, man. You can use HtmlHelper extension DisplayFor ONLY for model properties / fields. rm.GetString(item.Rating) isn't property or field. It is a method that returns string value. DisplayFor means that you bind some html-markup to your Model to display it's value via Razor. All DisplayFor, ValidationMessageFor and other HTML-helper extensions are designed to use with Models. If you want to display resource just use direct output like @rm.GetString(item.Rating) – n.prokhorov Mar 15 '16 at 11:39