Asp.net mvc ASP.NET MVC 3列表<;T>;到IEnumerable<;选择列表项>;

Asp.net mvc ASP.NET MVC 3列表<;T>;到IEnumerable<;选择列表项>;,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,我目前对DropDownListFor()对象的填充方式不满意。我试图找到尽可能通用的填充IEnumerable的方法。这就是我目前所拥有的 助手: public static List<SelectListItem> ToSelectList(IDictionary<string,string> dictionaryItems, string selectedValue, string noSelection, bool search = false) {

我目前对DropDownListFor()对象的填充方式不满意。我试图找到尽可能通用的填充IEnumerable的方法。这就是我目前所拥有的

助手:

public static List<SelectListItem> ToSelectList(IDictionary<string,string> dictionaryItems, string selectedValue, string noSelection, bool search = false)
    {
        List<SelectListItem> items = new List<SelectListItem>();

        if (search)
        {
            items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) });
        }

        foreach (var item in dictionaryItems)
        {
            items.Add(new SelectListItem
            {
                Text = item.Key,
                Value = item.Value,
                Selected = selectedValue == item.Value ? true : false
            });
        }

        return items
            .OrderBy(l => l.Text)
            .ToList();
    }

您可以提供获取键和值的回调,然后使用它们。除此之外,您还可以将其创建为扩展方法:

扩展方法:

public static List<SelectListItem> ToSelectList<T>(this List<T> Items, Func<T, string> getKey, Func<T, string> getValue, string selectedValue, string noSelection, bool search = false)
    {
        List<SelectListItem> items = new List<SelectListItem>();

        if (search)
        {
            items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) });
        }

        foreach (var item in Items)
        {
            items.Add(new SelectListItem
            {
                Text = getKey(item),
                Value = getValue(item),
                Selected = selectedValue == getValue(item) ? true : false
            });
        }

        return items
            .OrderBy(l => l.Text)
            .ToList();
    }
List<Org>() parentOrganisations = // fetch here
model.Organisations = parentOrganisations.ToSelectList(org => org.ID.ToString(), 
                                                       org => org.OrganisationName,
                                                       "-1", 
                                                       "-- None -- ", 
                                                       true);
public static List to selectList(此列表项,Func getKey,Func getValue,string selectedValue,string noSelection,bool search=false)
{
列表项=新列表();
如果(搜索)
{
添加(新SelectListItem{Selected=true,Value=“-1”,Text=string.Format(“--{0}--”,noSelection)});
}
foreach(项目中的var项目)
{
items.Add(新建SelectListItem)
{
Text=getKey(项目),
值=获取值(项目),
Selected=selectedValue==getValue(项目)?真:假
});
}
退货项目
.OrderBy(l=>l.Text)
.ToList();
}
用法:

public static List<SelectListItem> ToSelectList<T>(this List<T> Items, Func<T, string> getKey, Func<T, string> getValue, string selectedValue, string noSelection, bool search = false)
    {
        List<SelectListItem> items = new List<SelectListItem>();

        if (search)
        {
            items.Add(new SelectListItem { Selected = true, Value = "-1", Text = string.Format("-- {0} --", noSelection) });
        }

        foreach (var item in Items)
        {
            items.Add(new SelectListItem
            {
                Text = getKey(item),
                Value = getValue(item),
                Selected = selectedValue == getValue(item) ? true : false
            });
        }

        return items
            .OrderBy(l => l.Text)
            .ToList();
    }
List<Org>() parentOrganisations = // fetch here
model.Organisations = parentOrganisations.ToSelectList(org => org.ID.ToString(), 
                                                       org => org.OrganisationName,
                                                       "-1", 
                                                       "-- None -- ", 
                                                       true);
List()父组织=//在此处获取
model.organizations=parentorganizations.ToSelectList(org=>org.ID.ToString(),
org=>org.organizationname,
"-1", 
“--没有--”,
正确的);

注意:我在SO编辑器中键入了这一点,因此您可能会有一些语法错误(尽管它们应该很容易解决)。

您只能在controller中执行以下操作:-

List<SelectListItem> dropdownItems = parentOrganisations
    .Select(item => new SelectListItem
    {
        Value = item.ID.ToString(),
        Text = item.OrganisationName,
        Selected = "-1" == item.ID.ToString() ? true : false
    })
    .ToList();
列表下拉项=父组织
.选择(项目=>new SelectListItem
{
Value=item.ID.ToString(),
Text=item.organizationname,
Selected=“-1”==item.ID.ToString()?真:假
})
.ToList();

谢谢!这正是我所希望的。
List<SelectListItem> dropdownItems = parentOrganisations
    .Select(item => new SelectListItem
    {
        Value = item.ID.ToString(),
        Text = item.OrganisationName,
        Selected = "-1" == item.ID.ToString() ? true : false
    })
    .ToList();