擴充方法IEnumerable<T>轉換為IList<SelectListItem> ,提供@Html.DropDownList使用

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   io   strong   for   

由於在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>)          

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.