扩展方法IEnumerable<T>转换为IList<SelectListItem> ,提供@Html.DropDownList使用

 

由于在MVC中经常会使用到@Html.DropDownList方法,而该方法接收的是List<SelectListItem> 参数,因此就想着写一个扩展方法,直接把IEnumerable转换为List<SelectListItem>类型,这样使用起来会比较方便

正式进入正文。

1、首先创建下面实体:

 //水果类
public class Fruit
{
public string Code { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}

2、编写扩展方法,把IEnumerable转换为List<SelectListItem>类型,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc; namespace Common
{
public static class Extensions
{
/// <summary>
/// 扩展方法,IEnumerable<T>转换为IList<SelectListItem>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data">带转换的数据</param>
/// <param name="Text"></param>
/// <param name="Value"></param>
/// <param name="selectValue"></param>
/// <param name="NewItem">传递过来的SelectListItem,如请选择……</param>
/// <returns></returns>
public static IList<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> data, Expression<Func<T, object>> Text, Expression<Func<T, object>> Value, string selectValue = "",SelectListItem NewItem=null) where T : class,new()
{
var list = new List<SelectListItem>();
if (NewItem != null)
{
list.Add(NewItem);
}
string _text = "";
string _value = "";
if (Text.Body is MemberExpression)
{
MemberExpression TextMember = (MemberExpression)Text.Body;
_text = TextMember.Member.Name;
}
else if (Text.Body is UnaryExpression)
{
UnaryExpression TextMember = (UnaryExpression)Value.Body;
_text = (TextMember.Operand as MemberExpression).Member.Name;
}
if (Value.Body is MemberExpression)
{
MemberExpression ValueMember = (MemberExpression)Text.Body;
_value = ValueMember.Member.Name;
}
else if (Value.Body is UnaryExpression)
{
UnaryExpression ValueMember = (UnaryExpression)Value.Body;
_value = (ValueMember.Operand as MemberExpression).Member.Name;
}
var type = new T().GetType();
var TextPropertyInfo = type.GetProperty(_text);
var ValuePropertyInfo = type.GetProperty(_value);
foreach (var item in data)
{
var selectItem = new SelectListItem() { Text = TextPropertyInfo.GetValue(item).ToString(), Value = ValuePropertyInfo.GetValue(item).ToString() };
if (!string.IsNullOrWhiteSpace(selectValue) && selectValue == selectItem.Value)
{
selectItem.Selected = true;
}
list.Add(selectItem);
} return list;
} }

3、调用方法如下:

ViewBag.Fruits = list.ToSelectListItem(it => it.Name, it => it.Color, "", new SelectListItem() { Text = "请选择水果", Value = "", Selected = true });

 @Html.DropDownList("Fruits ",ViewBag.Fruits as IList<SelectListItem>)          
 
 
标签: MVC

IEnumerable<T>转换为IList<SelectListItem>的更多相关文章

  1. 扩展方法IEnumerable<T>转换为IList<SelectListItem> ,提供@Html.DropDownList使用

    由于在MVC中经常会使用到@Html.DropDownList方法,而该方法接收的是List<SelectListItem> 参数,因此就想着写一个扩展方法,直接把IEnumerable转 ...

  2. IEnumerable<T> 转换为数组

    IEnumerable<User> userlist=xxxx; string[] ids=userlist.select(u=>u.id).toArray();

  3. IEnumerable,ICollection,IList,List之间的区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 // 摘要: // 公开枚举器,该枚举器 ...

  4. IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用

    IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和I ...

  5. C# IEnumerable与IQueryable ,IEnumerable与IList ,LINQ理解Var和IEnumerable

    原文:https://www.cnblogs.com/WinHEC/articles/understanding-var-and-ienumerable-with-linq.html 使用LINQ从数 ...

  6. 对象列表转换为DataTable或DataTable转换为对象列表.

    /**********************************************************************************/ // 说明: 数据转换工具. ...

  7. IEnumerable和List有什么区别?

    如下.IList接口可以使用更多的方法.比如你看一个集合是否包含相应实体, IEnumerable不行,而 IList里有Contains,相应的实现了IList的可以添加,删除相应实体.而IEnum ...

  8. DataTable转换为List<Model>的通用类

    在开发中,把查询结果以DataTable返回很方便,但是在检索数据时又很麻烦,没有模型类型检索方便. 所以很多人都是按照以下方式做的: // 获得查询结果DataTable dt = DbHelper ...

  9. MVC学习笔记--IEnumerable的用法

    IEnumerable的用法 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现 并支持非泛型方法的简单的 ...

随机推荐

  1. SpringAccess数据库(oracle)构造

    陈科朝:http://blog.csdn.net/u013474104/article/details/44279309 ================ 1.spring 对数据库訪问的支持 当我们 ...

  2. 大数据系列修炼-Scala课程01

    简介 由于本人刚毕业,也是从事软件开发相关的工作.想再学习一下关于大数据.移动互联网.云计算相关的技术.为我的未来打好基础.并且从零开始学习大数据相关的知识,脚踏实地的走好每一步,听行业前辈说毕业生刚 ...

  3. 采用jquery的imgAreaSelect样品图像裁剪示范插件实现

    将用户上传的图片进行裁剪再保存是如今web2.0应用中经常处理的工作,如今借助jquery的imgareaselect插件再配合PHP的GD库就能够轻松的实现这个在曾经来说很棘手的功能. 我们来看看它 ...

  4. javascript/jquery读取和修改HTTP headers

    javascript/jquery读取和修改HTTP headers jquery修改HTTP headers jQuery Ajax可以通过headers或beforeSend修改request的H ...

  5. 从头开始学JavaScript (十)——垃圾收集

    原文:从头开始学JavaScript (十)--垃圾收集 一.垃圾收集 1.1javascript垃圾收集机制: 自动垃圾收集,执行环境会负责管理代码执行过程中的使用的内存.而在C和C++之类的语言中 ...

  6. HDU 1686 Oulipo(kmp)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  7. BCM策略路由交换芯片

    BCM几个交换芯片的寄存器和相关的路由 EGR_L3_NEXT_HOP.EGR_L3_INTF.ING_L3_NEXT_HOP BCM XGS系列SDK中和路由相关的几个命令 l3 l3table. ...

  8. MAC 命令行工具(Command Line Tools)安装

    不过升级后安装命令行工具(Command Line Tools)时发现官网没有clt的下载安装包了,原来改了,使用命令在线安装. 打开终端,输入命令:xcode-select --install 选择 ...

  9. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

  10. C控制语句--分支和跳转

    /*C控制语句--分支和跳转*/ /*关键字 if else switch continue break case default goto 运算符:&&(且) ||(或) ?:(三元 ...