2017-06-20 27 views
0

我有一個MVC項目。在視圖中,我使用ViewContext像這樣從資源文件加載值。ViewContext在當前上下文中不存在

@ViewContext.HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel") 

我發現了一個錯誤 - The name ViewContext does not exit in current context

以下是我在頁面上添加了命名空間。

@using System.Configuration 
@using System.Globalization 
@using System.Web.Configuration 
@using Telerik.Sitefinity.Utilities.Lucene.Net.Documents 
@using Telerik.Sitefinity.UI.MVC 

我不知道爲什麼它給了我這個錯誤。如果我運行錯誤的項目,它會顯示沒有任何問題的頁面。我也從資源文件中獲取值。

但我不知道如何解決它。

謝謝。

回答

0

您不需要使用ViewContext。在你的意見,你可以直接訪問資源,這樣的:

@System.Web.HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel") 

或者你可以使用這樣的:

@HttpContext.GetGlobalResourceObject("XyzResources", "MonLabel") 

但Sitefinity有更好的方式來處理與資源本地化。

在Sitefintiy後端,您可以管理資源標籤。 (在Administration->Labels & Messages) 然後,在視圖中可以像訪問:

@Html.Resource("AmazonSearchServiceTitle", "ProductsResources") 

您可以找到有關Sitefinity資源這篇文章中的詳細信息:http://docs.sitefinity.com/for-developers-create-custom-resource-classes

+0

感謝您爲我提供的鏈接。我試圖使用'@ Html.Resource'它給了我Html助手不包含資源定義和最佳擴展方法重載本地化helpers.resource,你能幫助我嗎? – ISHIDA

+0

你需要使用'@using Telerik.Sitefinity.Frontend.Mvc.Helpers'來添加 –

+0

我做到了。它沒有幫助 – ISHIDA

相關問題