ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字
標(biāo)題可能無法表達我的本意。比如,有這樣一個枚舉:
public enum MyChoice { MyFirstChoice = 0, MySecondChoice =1, MyThirdChoice = 2 }
數(shù)據(jù)庫中,某表某字段保存值為"0,1,2",在顯示的時候,我們希望是"第一個選擇,第二個選擇,第三個選擇"。如何做呢?
可以為枚舉項上面標(biāo)注自定義特性。先自定義一個特性如下:
public class EnumDisplayNameAttribute : Attribute { private string _displayName; public EnumDisplayNameAttribute(string displayName) { _displayName = displayName; } public string DisplayName { get { return _displayName; } } }
然后,把自定義特性標(biāo)注放到枚舉項上去。
public enum MyChoice { [EnumDisplayName("我的第一個選擇")] MyFirstChoice = 0, [EnumDisplayName("我的第二個選擇")] MySecondChoice =1, [EnumDisplayName("我的第三個選擇")] MyThirdChoice = 2 }
現(xiàn)在,需要一個幫助方法,能讀出枚舉項上的自定義特性EnumDisplayName。
public class EnumExt { /// <summary> /// 獲取枚舉項的注釋 /// </summary> /// <param name="e">枚舉項</param> /// <returns></returns> public static string GetEnumDescription(object e) { //獲取枚舉項 Type t = e.GetType(); //獲取枚舉項的字段 FieldInfo[] fis = t.GetFields(); foreach (FieldInfo fi in fis) { //如果當(dāng)前字段名稱不是當(dāng)前枚舉項 if (fi.Name != e.ToString()) { continue;//結(jié)束本次循環(huán) } //如果當(dāng)前字段的包含自定義特性 if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true)) { //獲取自定義特性的屬性值 return (fi.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName; } } return e.ToString(); } public static List<SelectListItem> GetSelectList(Type enumType) { List<SelectListItem> selectList = new List<SelectListItem>(); //selectList.Add(new SelectListItem{Text = "--請選擇--",Value = ""}); foreach (object e in Enum.GetValues(enumType)) { selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() }); } return selectList; } }
以上,
GetEnumDescription方法根據(jù)枚舉項獲取其上的自定義特性EnumDisplayNameAttribute的DisplayName屬性值。
GetSelectList方法根據(jù)枚舉的Type類型返回SelectListItem集合,通常在ASP.NET MVC中使用。
最后,就能實現(xiàn)本篇的需求:
static void Main(string[] args) { string myChoiceInt = "0,1,2"; string[] choiceArr = myChoiceInt.Split(','); string temp = string.Empty; foreach (string item in choiceArr) { //轉(zhuǎn)換成枚舉的類型 short enumValShort = short.Parse(item); temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ","; } Console.WriteLine(temp.Substring(0, temp.Length - 1)); Console.ReadKey(); }
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- 使用EF Code First搭建簡易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫遷移
- asp.net mvc CodeFirst模式數(shù)據(jù)庫遷移步驟詳解
- asp.net實現(xiàn)的MVC跨數(shù)據(jù)庫多表聯(lián)合動態(tài)條件查詢功能示例
- asp.net mvc 從數(shù)據(jù)庫中讀取圖片的實現(xiàn)代碼
- asp.net MVC 根據(jù)菜單樹類別不同動態(tài)加載視圖的實現(xiàn)步驟
- ASP.NET?MVC使用jQuery的Load方法加載靜態(tài)頁面及注意事項
- ASP.NET Mvc開發(fā)之EF延遲加載
- ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫信息
相關(guān)文章
C#命名空間System.ComponentModel屬性方法匯總
本文詳細講解了C#命名空間System.ComponentModel屬性方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01那些年,我還在學(xué)習(xí)asp.net(二) 學(xué)習(xí)筆記
那些年覺得看視頻是很輕松的了解一個東西,但是這樣的不足就是感覺太慢了,沒有看書來得快,所以在有了一些了解后,還得看點書,也許書上的不一定好,但書上會把每一個應(yīng)該說到的地方說到,好有個初步的認識2012-03-03微軟 Visual Studio 2010官方下載地址給大家
昨天VS2010在網(wǎng)上報道都已經(jīng)發(fā)布了,現(xiàn)在今天在網(wǎng)上找到Visual Studio 2010官方下載地址,提供給大家下載。2010-04-04asp.net學(xué)習(xí)中發(fā)現(xiàn)的比較完整的流程
總結(jié)的非常不錯的asp.net學(xué)習(xí)資料,方便想自學(xué)asp.net的朋友2008-08-08ASP.NET?Core中Startup類、Configure()方法及中間件詳解
本文詳細講解了ASP.NET?Core中Startup類、Configure()方法及中間件,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01