2017-10-09 43 views
0

我有一個枚舉下拉本地化EnumDropDownListFor使用asp.net樣板

//control 
@Html.EnumDropDownListFor(
         m => m.OrderBy, 
         new {@class = "btn btn-default dropdown-toggle toggle", onchange = "document.getElementById('hf_Pagename').value,this.form.submit();"}) 

//my enum 
public enum OrderByOptions 
    { 
     Default, 
     PriceLowToHigh, 
     PriceHighToLow, 
     MostRecent 
    } 

現在的問題是我需要本地化它們。但在這種情況下,從 「PriceLowToHigh」 需要改變,以 「價格 - 從低到高」

回答

0

您可以使用AbpDisplayNameAttribute

public enum OrderByOptions 
{ 
    [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.Default")] 
    Default, 
    [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceLowToHigh")] 
    PriceLowToHigh, 
    [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceHighToLow")] 
    PriceHighToLow, 
    [AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.MostRecent")] 
    MostRecent 
} 

定義他們在您的本地化文件:

<text name="OrderByOptions.PriceLowToHigh">Price - Low to High</text> 

更新

AbpDisplayName適用於類型類

您可以定義:

[AttributeUsage(AttributeTargets.Field)] 
public class FieldAbpDisplayNameAttribute : AbpDisplayNameAttribute 
{ 
    // ... 
} 

然後使用[FieldAbpDisplayNameAttribute(...)]代替。

+0

您好,感謝阿龍,但AbpDisplayName適用於類型的類。那是錯誤我得到 –

+0

流利,我用數據註釋[顯示],它的工作。謝謝Aaron –

+0

查看更新的答案。 '[Display]'需要一個'const'字符串,對吧? – aaron

0

有很多方法可以解決這個問題。

Way#1 請勿使用@ Html.EnumDropDownListFor!只需遍歷枚舉並像下面那樣創建html元素; (我寫我的頭頂部的代碼)

<select> 
@foreach (var item in Enum.GetValues(typeof(OrderByOptions))) 
{ 
    <option value="@((int)item)">@(Localize(item.ToString()))</option> 
} 
</select> 

有沒有本地化的方法。只需用你的方式本地化。

路#2

另一種方法是不使用枚舉,但創建一個下拉項集合。讓一個項目由DisplayText和Value組成。顯示文字必須從服務器本地化。

路#3

按照說明這裏解釋: https://ruijarimba.wordpress.com/2012/02/17/asp-net-mvc-creating-localized-dropdownlists-for-enums/