如何过滤枚举并在下拉列表中使用 [英] How to Filter Enum and Use it in Dropdown
本文介绍了如何过滤枚举并在下拉列表中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是MVC 5
的新手,我的目标是过滤要显示在下拉列表中的enum
中的列表
I am new to MVC 5
and My goal is to filter the list in my enum
that I will show in my dropdown list
public enum DayofWeekType
{
Monday=1,
Tuesday= 2,
Wednesday=3,
Thursday=4,
Friday= 5,
Saturday=6,
Sunday= 7
}
当登录的用户不是管理员时,我只想显示周五,周六和周日的下拉列表,我找不到在Model
中过滤enum
字段的解决方案,尝试在模型中添加条件,但始终总结为错误.尝试搜索LINQ
和jQuery
解决方案.
And I just want to show is friday,saturday and sunday in dropdown when the logged user is Not Administrator, I cant find solution on filtering enum
field in Model
, tried adding Conditions in model but always sums up with errors. Tried searching for LINQ
and jQuery
solutions.
推荐答案
您可以这样做
var enumlist = Enum.GetValues(typeof(DayofWeekType)).Cast<DayofWeekType>().Select(v => new SelectListItem
{
Text = v.ToString(),
Value = ((int)v).ToString()
});
if (IsUser) //your condition here
{
enumlist= enumlist.Skip(4);
}
ViewBag.enumlist = enumlist;
在您看来
@Html.DropDownListFor(x=>x.Id,(IEnumerable<SelectListItem>) ViewBag.enumlist)
.Skip
将跳过第一个4
值,并以Friday
值Friday
.Skip
will skip first 4
values and start with 5th
value which is Friday
这篇关于如何过滤枚举并在下拉列表中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文