andrew4582

ListDataItem

Aug 15th, 2010
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Mvc;
  4.  
  5. namespace WebSite1 {
  6.    
  7.  
  8.     public class ListDataItem<TKey> {
  9.         string _display;
  10.         TKey _key;
  11.        
  12.         public TKey Key {
  13.             get { return _key; }
  14.             set { _key = value; }
  15.         }
  16.        
  17.         public string Display {
  18.             get { return _display; }
  19.             set { _display = value; }
  20.         }
  21.         public override string ToString () {
  22.             return Display;
  23.         }
  24.         public ListDataItem () {}
  25.         public ListDataItem (TKey key_, string display_) {
  26.             Key = key_;
  27.             Display = display_;
  28.         }
  29.         public ListDataItem (string display_) {
  30.             Display = display_;
  31.             Key = default(TKey);
  32.         }
  33.        
  34.        
  35.         #region Static
  36.        
  37.         public static List<SelectListItem> ToSelectList(ListDataItem<TKey>[] items){
  38.            
  39.             List<SelectListItem> selectList = new List<SelectListItem>();
  40.            
  41.             foreach (var item in items) {
  42.                 selectList.Add(new SelectListItem(){
  43.                     Text = item.Display,
  44.                     Value = item.Key.ToString(),
  45.                     Selected = false,
  46.                 });
  47.             }
  48.             return selectList;
  49.         }
  50.        
  51.         #endregion
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment