Select Item Attribute improvements

pull/272/head
yekalkan 7 years ago
parent 0e5d126ebf
commit 48512ca236

@ -71,20 +71,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers
public static T GetAttribute<T>(ModelExplorer property) where T : Attribute
{
//var xd1 = property.Container.ModelType.GetTypeInfo();
//var xd2 = xd1.GetProperty(property.Metadata.PropertyName);
var xd1 = property.Metadata.ContainerType.GetTypeInfo();
var xd2 = xd1.GetProperty(property.Metadata.PropertyName);
if (xd2 == null)
{
return null;
}
var attribute = xd2 .GetCustomAttribute<T>();
return attribute;
return property?.Metadata?.ContainerType?.GetTypeInfo()?.GetProperty(property.Metadata.PropertyName)?.GetCustomAttribute<T>();
}
}
}

@ -1,19 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
[AttributeUsage(AttributeTargets.Property)]
public class SelectItems: Attribute
{
public IEnumerable<SelectListItem> Items { get; set; }
public string ItemsListPropertyName { get; set; }
public SelectItems(IEnumerable<SelectListItem> items)
public Type EnumType { get; set; }
public SelectType SelectType { get; set; } = SelectType.Dropdown;
public IEnumerable<SelectListItem> GetItems(ModelExplorer explorer)
{
if (IsEnumItem())
{
return GetItemsFromEnum();
}
else
{
return GetItemsFromListField(explorer);
}
}
private IEnumerable<SelectListItem> GetItemsFromListField(ModelExplorer explorer)
{
var properties = explorer.Properties.Where(p => p.Metadata.PropertyName.Equals(ItemsListPropertyName)).ToList();
return properties.Count > 0
? properties.First().Model as IEnumerable<SelectListItem>
: new List<SelectListItem>();
}
private IEnumerable<SelectListItem> GetItemsFromEnum()
{
var enumItems = EnumType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static);
return enumItems.Select((t, i) => new SelectListItem() { Value = i.ToString(), Text = t.Name }).ToList();
}
private bool IsEnumItem()
{
Items = items;
return EnumType != null && EnumType.GetTypeInfo().IsEnum;
}
}
}

@ -0,0 +1,8 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
{
public enum SelectType
{
Dropdown,
Radio
}
}
Loading…
Cancel
Save