如何将枚举的值放入 SelectList 中
想象一下我有一个像这样的枚举(仅作为示例):
public enum Direction{
Horizontal = 0,
Vertical = 1,
Diagonal = 2
}
考虑到枚举的内容将来可能会发生变化,如何编写一个例程将这些值放入 System.Web.Mvc.SelectList 中? 我想将每个枚举名称作为选项文本,并将其值作为值文本,如下所示:
<select>
<option value="0">Horizontal</option>
<option value="1">Vertical</option>
<option value="2">Diagonal</option>
</select>
这是迄今为止我能想到的最好的:
public static SelectList GetDirectionSelectList()
{
Array values = Enum.GetValues(typeof(Direction));
List<ListItem> items = new List<ListItem>(values.Length);
foreach (var i in values)
{
items.Add(new ListItem
{
Text = Enum.GetName(typeof(Direction), i),
Value = i.ToString()
});
}
return new SelectList(items);
}
但是,这总是将选项文本呈现为“System.Web.Mvc”。项目清单'。 通过此调试还显示 Enum.GetValues() 返回“水平、垂直”等,而不是我预期的 0、1,这让我想知道 Enum.GetName() 和 Enum 之间有什么区别。获取值()。
Imagine I have an enumeration such as this (just as an example):
public enum Direction{
Horizontal = 0,
Vertical = 1,
Diagonal = 2
}
How can I write a routine to get these values into a System.Web.Mvc.SelectList, given that the contents of the enumeration are subject to change in future? I want to get each enumerations name as the option text, and its value as the value text, like this:
<select>
<option value="0">Horizontal</option>
<option value="1">Vertical</option>
<option value="2">Diagonal</option>
</select>
This is the best I can come up with so far:
public static SelectList GetDirectionSelectList()
{
Array values = Enum.GetValues(typeof(Direction));
List<ListItem> items = new List<ListItem>(values.Length);
foreach (var i in values)
{
items.Add(new ListItem
{
Text = Enum.GetName(typeof(Direction), i),
Value = i.ToString()
});
}
return new SelectList(items);
}
However this always renders the option text as 'System.Web.Mvc.ListItem'. Debugging through this also shows me that Enum.GetValues() is returning 'Horizontal, Vertical' etc. instead of 0, 1 as I would've expected, which makes me wonder what the difference is between Enum.GetName() and Enum.GetValue().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我已经有一段时间没有这样做了,但我认为这应该有效。
It's been awhile since I've had to do this, but I think this should work.
ASP.NET MVC 5.1 中为此提供了一个新功能。
http://www.asp.net/mvc/overview/releases/mvc51-release-笔记#Enum
There is a new feature in ASP.NET MVC 5.1 for this.
http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Enum
这就是我刚刚制作的,我个人认为它很性感:
我最终将做一些翻译工作,这样 Value = enu.ToString() 就会在某个地方调用某些东西。
This is what I have just made and personally I think its sexy:
I am going to do some translation stuff eventually so the Value = enu.ToString() will do a call out to something somewhere.
要获取枚举的值,您需要将枚举转换为其基础类型:
To get the value of an enum you need to cast the enum to its underlying type:
我想做一些与 Dann 的解决方案非常相似的事情,但我需要 Value 为 int,文本为 Enum 的字符串表示形式。 这就是我想出的:
I wanted to do something very similar to Dann's solution, but I needed the Value to be an int and the text to be the string representation of the Enum. This is what I came up with:
在 ASP.NET Core MVC 中,这是通过 标签助手< 完成的/a>.
In ASP.NET Core MVC this is done with tag helpers.
也许不是问题的确切答案,但在 CRUD 场景中,我通常会实现这样的方法:在
View("Create") 和 View("Edit") 之前调用
PopulateViewdata4Selectlists
,然后在 View 中调用:就这样..
maybe not an exact answer to the question, but in CRUD scenarios i usually implements something like this:
PopulateViewdata4Selectlists
is called before View("Create") and View("Edit"), then and in the View:and that's all..
或者:
Or:
由于各种原因,我有更多的类和方法:
枚举项目集合
在控制器中创建选择列表
MyView.cshtml
I have more classes and methods for various reasons:
Enum to collection of items
Creating selectlist in Controller
MyView.cshtml
我有很多枚举选择列表,经过大量搜索和筛选后,发现制作通用助手最适合我。 它返回索引或描述符作为选择列表值,以及描述作为选择列表文本:
Enum:
Helper:
Usage:
将索引参数设置为“true”作为值:
I had many enum Selectlists and, after much hunting and sifting, found that making a generic helper worked best for me. It returns the index or descriptor as the Selectlist value, and the Description as the Selectlist text:
Enum:
Helper:
Usage:
Set parameter to 'true' for indices as value:
您可以使用此帮助器将枚举类转换为值和显示名称列表(您必须之前编写 GetDisplalyName 扩展):
you can use this helper for convert the enum class to List of value and display name (you have to write the GetDisplalyName extension before):