REORG
This commit is contained in:
parent
8d68ee380a
commit
45514010f2
3505 changed files with 154 additions and 523 deletions
52
src/Org/Helpers/PageHelpers.cs
Normal file
52
src/Org/Helpers/PageHelpers.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
namespace Yavsc.Server.Helpers
|
||||
{
|
||||
public static class PageHelpers
|
||||
{
|
||||
public static List<SelectListItem> CreateSelectListItems<T> (this IStringLocalizer<T> localisation, Type enumType, object selectedValue =null)
|
||||
{
|
||||
string selectedName = (selectedValue != null) ? enumType.GetEnumName(selectedValue) : null;
|
||||
|
||||
var items = new List<SelectListItem> ();
|
||||
var names = enumType.GetEnumNames();
|
||||
var values = enumType.GetEnumValues();
|
||||
for (int index = 0; index < names.Length; index++)
|
||||
{
|
||||
var itemName = names[index];
|
||||
items.Add(new SelectListItem() {
|
||||
Value = values.GetValue(index).ToString(), Text = localisation[itemName], Selected = ( itemName == selectedName)
|
||||
}) ;
|
||||
}
|
||||
var list = new SelectList(items);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static List<SelectListItem> AddNull(this List<SelectListItem> selectList, string displayNull, object selectedValue = null)
|
||||
{
|
||||
selectList.Add(new SelectListItem { Text = displayNull, Value = "", Selected = selectedValue == null });
|
||||
return selectList;
|
||||
}
|
||||
|
||||
public static List<SelectListItem> CreateSelectListItems<T> (this IEnumerable<T>data,
|
||||
Func<T,string> dataField,
|
||||
Func<T,string> displayField = null, object selectedValue =null) where T : class
|
||||
{
|
||||
if (displayField == null) displayField = dataField;
|
||||
var items = new List<SelectListItem> ();
|
||||
foreach (var dataItem in data)
|
||||
{
|
||||
var itemVal = dataField(dataItem);
|
||||
var itemName = displayField(dataItem);
|
||||
items.Add(new SelectListItem() {
|
||||
Value = itemVal, Text = itemName, Selected = ( selectedValue?.Equals(itemVal) ?? false )
|
||||
}) ;
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue