簡體   English   中英

枚舉到列表<Object> (身份證,姓名)

[英]Enum to List<Object> (Id, Name)

enum轉換為 Id/Name 對象列表的最佳實踐是什么?

Enum

public enum Type
{
    Type1= 1,
    Type2= 2,
    Type3= 3,
    Type4= 4
}

Object

public class TypeViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

類似的東西:

var typeList = new List<TypeViewModel>();
foreach (Type type in Enum.GetValues(typeof(Type)))
{
    typeList.Add(new TypeViewModel(type.Id, type.Name));
}

使用LINQ

var typeList = Enum.GetValues(typeof(Type))
               .Cast<Type>()
               .Select(t => new TypeViewModel
               {
                   Id = ((int)t),
                   Name = t.ToString()
               });

結果:

在此處輸入圖片說明

假設我們有:

public enum Gender
{
   Male = 0,
   Female = 1
}

和模型:

public class Person 
{
   public int Id {get; set;}
   public string FullName {get; set;}
   public Gender Gender {get; set;}
}

鑒於您可以簡單地使用:

@model YourNameSpaceWhereModelsAre.Person;

... 

@Html.BeginForm(...)
{
   @Html.HiddenFor(model => model.Id);
   @Html.EditorFor(model => model.FullName);
   @Html.EnumDropDownListFor(m => Model.Gender);

   <input type="submit"/>
}

您可以找到更多信息或MSDN

將枚舉轉換為列表<object><div id="text_translate"><p>首先,為了避免下意識的<em>“這是重復的”</em>陳述:關於序列化枚舉有很多關於 SO 的問題。 但沒有一個(我發現)處理具有描述的枚舉,或者嘗試將枚舉轉換為特定的 object。 (如果你能找到我錯過的一個,那么我會接受這是重復的。)</p><p> 給定一個看起來像這樣的枚舉:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想將其轉換為 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有這兩個類:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一個看起來像這樣的調用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(這是我需要幫助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已經有一個獲取描述的 Enum 擴展,所以我不需要幫助。 我遇到的問題是thisEnum1和thisEnum2最終的值為0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此處輸入圖像描述"></a></p><p> ( DistrictType是 Enum 的真實名稱。)</p><p> 如何獲得可以循環的枚舉的實際實例?</p></div></object>

[英]Convert an Enum to a List<object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 我想要一個通用方法來獲取枚舉並從枚舉設置 ID 和 Name 屬性創建類型對象列表 將枚舉轉換為列表<object><div id="text_translate"><p>首先,為了避免下意識的<em>“這是重復的”</em>陳述:關於序列化枚舉有很多關於 SO 的問題。 但沒有一個(我發現)處理具有描述的枚舉,或者嘗試將枚舉轉換為特定的 object。 (如果你能找到我錯過的一個,那么我會接受這是重復的。)</p><p> 給定一個看起來像這樣的枚舉:</p><pre> public enum OptionType { [Description("Option A")] OptionA = 1, [Description("Option B")] OptionB = 2, Description("Option C")] OptionC= 3, }</pre><p> 我想將其轉換為 JSON 字符串,如下所示:</p><pre> [ { "Id": "1", "Description": "Option A"}, { "Id": "2", "Description": "Option B"}, { "Id": "3", "Description": "Option C"} ]</pre><p> 我有這兩個類:</p><pre> public class EnumList { public List&lt;EnumNameValue&gt; EnumNamesValues { get; set; } } public class EnumNameValue { public int Id { get; set; } public string Description { get; set; } }</pre><p> 我有一個看起來像這樣的調用方法:</p><pre> EnumList enumList = EnumSerializer.EnumToJson&lt;OptionType&gt;();</pre><p> 最后(這是我需要幫助的地方):</p><pre> public static class EnumSerializer { public static EnumList EnumToJson&lt;T&gt;() where T: Enum { Type enumType = typeof(T); // &lt;--- WORKS.; Enum thisEnum1 = (T)Activator.CreateInstance(enumType); // &lt;--- DOES NOT WORK Enum thisEnum2 = (Enum)Activator,CreateInstance(enumType); // &lt;--- DOES NOT WORK // If I can get this far, I *THINK* I can figure it out from here } }</pre><p> 我已經有一個獲取描述的 Enum 擴展,所以我不需要幫助。 我遇到的問題是thisEnum1和thisEnum2最終的值為0</p><p> <a href="https://i.stack.imgur.com/zRatA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zRatA.png" alt="在此處輸入圖像描述"></a></p><p> ( DistrictType是 Enum 的真實名稱。)</p><p> 如何獲得可以循環的枚舉的實際實例?</p></div></object> 根據對象的枚舉屬性排序列表 如何檢查對象是否為列表枚舉類型的對象? 如何動態地使用枚舉列表對對象執行foreach 如何從枚舉創建 object 列表 將 object 轉換為 List&lt;(Enum, string)&gt; 問題 在.NET Core中顯示對應ID的枚舉名稱值 按名稱獲取營銷列表ID 獲取綁定到下拉列表的枚舉的ID
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM