2011-12-07 64 views
2

我上的MVC 3視圖編寫這些代碼:爲什麼我的MVC Razor視圖不希望實例化一個ResourceManager

<table> 
    <tr> 
     <th>@SecondIndexStrings.Activity_ActivityName</th> 
     <th>@SecondIndexStrings.Activity_DateStarted</th> 
     <th>@SecondIndexStrings.Activity_ProficiencyLevel</th> 
    </tr> 
    @foreach (var activity in Model.Activities) 
    { 
     <tr> 
      <td>@activity.ActivityName</td> 
      <td>@activity.DateStarted.ToShortDateString()</td> 
      <td>@new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString())</td> 
     </tr> 
    } 
</table> 

出於某種原因,符合「@new的ResourceManager ...」是沒有被識別爲有效,每當我運行它時,它都會給我一個編譯錯誤。

如果我只寫@ResourceManager,文本變成藍色,表明它被認爲是一個有效的類,對於IndexStrings資源來說也是一樣的,但是就整個事情而言,似乎它並不知道那些應該是類。

我在做什麼錯?

謝謝。

回答

5

因爲空白的,你必須使用括號,以幫助它看到你的表情從哪裏開始/結束:

@(new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString())) 

您可能還需要考慮增加一個輔助方法,也許作爲一個擴展法在HtmlHelper,所以你可以使用類似:

@Html.Resource<IndexStrings>(activity.ProficiencyLevel) 

或相似,這可能看起來像:

public static string Resource<T>(this HtmlHelper html, object key) { 
    return new ResourceManager(typeof(T)).GetString(key.ToString()); 
} 
+0

非常感謝。 –

相關問題