C# 从所选枚举值ASP.NET MVC生成枚举
我有一个用于保存帖子类型的枚举:C# 从所选枚举值ASP.NET MVC生成枚举,c#,asp.net-mvc-4,enums,html.dropdownlistfor,C#,Asp.net Mvc 4,Enums,Html.dropdownlistfor,我有一个用于保存帖子类型的枚举: public enum PostType { PostType1, PostType2, PostType3, PostType4, PostType5, PostType6 } 我也有一些用户角色,所以根据他们的角色,用户可以添加允许的帖子 所以我想从选定的枚举值构建下拉列表 例如: 对于UserType1,我的enum dropdownlist将只包含posttype1,对于UserType4,所有都是允许的
public enum PostType
{
PostType1,
PostType2,
PostType3,
PostType4,
PostType5,
PostType6
}
我也有一些用户角色,所以根据他们的角色,用户可以添加允许的帖子
所以我想从选定的枚举值构建下拉列表
例如:
对于UserType1,我的enum dropdownlist将只包含posttype1,对于UserType4,所有都是允许的
如何在ViewModel中实现这一点
提前谢谢你…你可以这样做
using System.Reflection;
using System.ComponentModel;
using System.Linq.Expressions;
namespace MvcApplication7.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
public static class Helper {
public static HtmlString CreateDropDown(this HtmlHelper helper, Type enumType)
{
SelectList list = ToSelectList(typeof(PostType));
string Markup = @"<select>";
foreach(var item in list){
string disable = item.Value == "1" ? "disabled" : ""; //eavluate by yourself set it to disabled or not by user role just set a dummy condition
Markup += Environment.NewLine + string.Format("<option value='{0}' {1}>{2}</option>",item.Value,disable,item.Text);
}
Markup += "</select>";
return new HtmlString(Markup);
}
public static SelectList ToSelectList(Type enumType)
{
var items = new List<SelectListItem>();
foreach (var item in Enum.GetValues(enumType))
{
FieldInfo fi = enumType.GetField(item.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
var title = "";
if (attributes != null && attributes.Length > 0)
{
title = attributes[0].Description;
}
else
{
title = item.ToString();
}
var listItem = new SelectListItem
{
Value = ((int)item).ToString(),
Text = title,
};
items.Add(listItem);
}
return new SelectList(items, "Value", "Text");
}
}
public enum PostType
{
PostType1,
PostType2,
PostType3,
PostType4,
PostType5,
PostType6
}
}
试试这个,创建一个助手
namespace MvcApplication1.Helpers
{
public class ModelValueListProvider : IEnumerable<SelectListItem>
{
List<KeyValuePair<string, string>> innerList = new List<KeyValuePair<string, string>>();
public static readonly ModelValueListProvider PostTypeList = new PostTypeListProvider();
public static ModelValueListProvider MethodAccessEnumWithRol(int id)
{
return new PostTypeListProvider(null, id);
}
protected void Add(string value, string text)
{
string innerValue = null, innerText = null;
if (value != null)
innerValue = value.ToString();
if (text != null)
innerText = text.ToString();
if (innerList.Exists(kvp => kvp.Key == innerValue))
throw new ArgumentException("Value must be unique", "value");
innerList.Add(new KeyValuePair<string, string>(innerValue, innerText));
}
public IEnumerator<SelectListItem> GetEnumerator()
{
return new ModelValueListProviderEnumerator(innerList.GetEnumerator());
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private struct ModelValueListProviderEnumerator : IEnumerator<SelectListItem>
{
private IEnumerator<KeyValuePair<string, string>> innerEnumerator;
public ModelValueListProviderEnumerator(IEnumerator<KeyValuePair<string, string>> enumerator)
{
innerEnumerator = enumerator;
}
public SelectListItem Current
{
get
{
var current = innerEnumerator.Current;
return new SelectListItem { Value = current.Key, Text = current.Value };
}
}
public void Dispose()
{
try
{
innerEnumerator.Dispose();
}
catch (Exception)
{
}
}
object System.Collections.IEnumerator.Current
{
get
{
return Current;
}
}
public bool MoveNext()
{
return innerEnumerator.MoveNext();
}
public void Reset()
{
innerEnumerator.Reset();
}
}
private class PostTypeListProvider : ModelValueListProvider
{
public PostTypeListProvider(string defaultText = null, int rolId = 0)
{
if (!string.IsNullOrEmpty(defaultText))
Add(string.Empty, defaultText);
if (rolId == 1)
Add(PostType.PostType1, "PostType1");
else
{
Add(PostType.PostType2, "PostType2");
Add(PostType.PostType3, "PostType3");
Add(PostType.PostType4, "PostType4");
Add(PostType.PostType5, "PostType5");
Add(PostType.PostType6, "PostType6");
}
}
public void Add(PostType value, string text)
{
Add(value.ToString("d"), text);
}
}
}
public enum PostType
{
PostType1,
PostType2,
PostType3,
PostType4,
PostType5,
PostType6
}
}
希望能够帮助您创建SelectListItems列表,并根据用户角色使用所需的值填充该列表。但是,该列表将不会针对该枚举进行强键入。我认为,根据所选类型,存在一个误解:我的意思是,我想从枚举创建一些dropdownlist,但基于角色禁用一些值。只需修改答案..看一看看那里,如果你觉得有用的话,你可以根据你的需要来改进。。
namespace MvcApplication1.Helpers
{
public class ModelValueListProvider : IEnumerable<SelectListItem>
{
List<KeyValuePair<string, string>> innerList = new List<KeyValuePair<string, string>>();
public static readonly ModelValueListProvider PostTypeList = new PostTypeListProvider();
public static ModelValueListProvider MethodAccessEnumWithRol(int id)
{
return new PostTypeListProvider(null, id);
}
protected void Add(string value, string text)
{
string innerValue = null, innerText = null;
if (value != null)
innerValue = value.ToString();
if (text != null)
innerText = text.ToString();
if (innerList.Exists(kvp => kvp.Key == innerValue))
throw new ArgumentException("Value must be unique", "value");
innerList.Add(new KeyValuePair<string, string>(innerValue, innerText));
}
public IEnumerator<SelectListItem> GetEnumerator()
{
return new ModelValueListProviderEnumerator(innerList.GetEnumerator());
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private struct ModelValueListProviderEnumerator : IEnumerator<SelectListItem>
{
private IEnumerator<KeyValuePair<string, string>> innerEnumerator;
public ModelValueListProviderEnumerator(IEnumerator<KeyValuePair<string, string>> enumerator)
{
innerEnumerator = enumerator;
}
public SelectListItem Current
{
get
{
var current = innerEnumerator.Current;
return new SelectListItem { Value = current.Key, Text = current.Value };
}
}
public void Dispose()
{
try
{
innerEnumerator.Dispose();
}
catch (Exception)
{
}
}
object System.Collections.IEnumerator.Current
{
get
{
return Current;
}
}
public bool MoveNext()
{
return innerEnumerator.MoveNext();
}
public void Reset()
{
innerEnumerator.Reset();
}
}
private class PostTypeListProvider : ModelValueListProvider
{
public PostTypeListProvider(string defaultText = null, int rolId = 0)
{
if (!string.IsNullOrEmpty(defaultText))
Add(string.Empty, defaultText);
if (rolId == 1)
Add(PostType.PostType1, "PostType1");
else
{
Add(PostType.PostType2, "PostType2");
Add(PostType.PostType3, "PostType3");
Add(PostType.PostType4, "PostType4");
Add(PostType.PostType5, "PostType5");
Add(PostType.PostType6, "PostType6");
}
}
public void Add(PostType value, string text)
{
Add(value.ToString("d"), text);
}
}
}
public enum PostType
{
PostType1,
PostType2,
PostType3,
PostType4,
PostType5,
PostType6
}
}
@Html.DropDownListFor(m => Model.idRoleuser, new SelectList(MvcApplication1.Helpers.ModelValueListProvider.MethodAccessEnumWithRol(1), "Value", "Text"))
@Html.DropDownListFor(m => Model.idRoleuser, new SelectList(MvcApplication1.Helpers.ModelValueListProvider.MethodAccessEnumWithRol(2), "Value", "Text"))