【发布时间】:2017-02-07 11:34:19
【问题描述】:
我是 C# 新手。最近我在一个项目上遇到了问题。我需要使用枚举列表生成下拉列表。我找到了一个很好的工作sample。 但是该示例仅使用一个枚举,我的要求是将此代码用于任何枚举。我想不通。我的代码是
public List<SelectListItem> GetSelectListItems()
{
var selectList = new List<SelectListItem>();
var enumValues = Enum.GetValues(typeof(Industry)) as Industry[];
if (enumValues == null)
return null;
foreach (var enumValue in enumValues)
{
// Create a new SelectListItem element and set its
// Value and Text to the enum value and description.
selectList.Add(new SelectListItem
{
Value = enumValue.ToString(),
// GetIndustryName just returns the Display.Name value
// of the enum - check out the next chapter for the code of this function.
Text = GetEnumDisplayName(enumValue)
});
}
return selectList;
}
我需要将任何枚举传递给此方法。任何帮助表示感谢。
【问题讨论】:
-
能否请您添加重复答案的链接
标签: c# asp.net asp.net-mvc