你必須要在控制器級別對其進行過濾,按照下面的步驟來實現
int[] eliminateDays = null;
// wrap enum into collection
var enumDaysCollection = (from dayName in Enum.GetNames(typeof(DayofWeekType))
select new
{
Text = dayName.ToString(),
Value = (int)Enum.Parse(typeof(DayofWeekType), dayName)
}).ToList();
// array contain days (enum values) which will be ignored as per user specific
// lets say you want to ignore monday, tuesday, wednesday, thursday
if (User.IsInRole("SomeUserRole"))
{
eliminateDays = new int[] { 1, 2, 3, 4 };
}
else if (User.IsInRole("AnotherUserRole"))
{
eliminateDays = new int[] { 1, 3 };
}
else
{
//For Admin will make empty so that it will show all days
eliminateDays = new int[] { };
}
// filter collection
var dropDownItems = (from day in enumDaysCollection
let days = eliminateDays
where !days.Contains(day.Value)
select new SelectListItem
{
Text = day.Text,
Value = Convert.ToString(day.Value)
}).ToList();
//send dropdownlist values to view
ViewBag.DropDownItems = dropDownItems;
最後分配SelectListItem集合DROPDOWNLIST
@Html.DropDownList("DaysName", (List<SelectListItem>)ViewBag.DropDownItems, "Select Days..", new { @class = "dropdown" })
這只是枚舉定義開始。你的過濾代碼在哪裏?你的代碼有什麼問題?你有錯誤?值不被過濾?目前看起來你希望我們從頭到尾解決你的任務 –
你的模型是怎樣的? – MacakM
@SergeyBerezovskiy沒有先生,我只想知道如果在這裏可以顯示您的視圖代碼的工作環境 –