kreta/Kreta.BusinessLogic/Utils/ComboboxHelperExtensions.cs
2024-03-13 00:33:46 +01:00

74 lines
2.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kreta.BusinessLogic.Classes.ComboBox;
using Kreta.Framework.Caching;
namespace Kreta.BusinessLogic.Utils
{
public static class ComboBoxHelperExtensions
{
public static List<ComboBoxListItem> ToComboBoxItemList(this IDictionary<string, string> dictionary, List<string> removeValueList = null, List<string> onlyValueList = null)
{
var itemList = removeValueList != null
? dictionary.Where(x => !removeValueList.Contains(x.Key))
: dictionary;
var itemList2 = onlyValueList != null
? itemList.Where(x => onlyValueList.Contains(x.Key))
: itemList;
var result = itemList2.Select(x => new ComboBoxListItem
{
Value = x.Key,
Text = x.Value
}).ToList();
return result;
}
public static List<SelectListItem> ToSelectItemList(this IDictionary<string, string> dictionary, List<string> removeValueList = null, string selectedValuePart = null)
{
var itemList = removeValueList != null
? dictionary.Where(x => !removeValueList.Contains(x.Key))
: dictionary;
var result = itemList.Select(x => new SelectListItem
{
Value = x.Key,
Text = x.Value,
Selected = !string.IsNullOrWhiteSpace(selectedValuePart) && x.Value.ToLower().IndexOf("- " + selectedValuePart.ToLower()) > -1
}).ToList();
return result;
}
public static List<ComboBoxListItem> ToComboBoxItemList(this List<DictionaryItem> dictionaryList, List<int> removeValueList = null, List<int> onlyValueList = null)
{
var itemList = removeValueList != null ? dictionaryList.Where(x => !removeValueList.Contains(x.Id)) : dictionaryList;
var itemList2 = onlyValueList != null ? itemList.Where(x => onlyValueList.Contains(x.Id)) : itemList;
var result = itemList2.Select(x => new ComboBoxListItem
{
Value = x.Id.ToString(),
Text = x.Name
}).ToList();
return result;
}
public static List<SelectListItem> ToSelectItemList(this List<DictionaryItem> dictionaryList, List<int> removeValueList = null, string selectedValuePart = null)
{
var itemList = removeValueList != null
? dictionaryList.Where(x => !removeValueList.Contains(x.Id))
: dictionaryList;
var result = itemList.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name,
Selected = !string.IsNullOrWhiteSpace(selectedValuePart) && x.Name.ToLower().IndexOf("- " + selectedValuePart.ToLower()) > -1
}).ToList();
return result;
}
}
}