diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs index 5e2be762ba..2b8f15118a 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/AbpTagHelperService.cs @@ -71,20 +71,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers public static T GetAttribute(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(); - - return attribute; + return property?.Metadata?.ContainerType?.GetTypeInfo()?.GetProperty(property.Metadata.PropertyName)?.GetCustomAttribute(); } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectItems.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectItems.cs index 2b0eb37566..b1e29b1a32 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectItems.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectItems.cs @@ -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 Items { get; set; } + public string ItemsListPropertyName { get; set; } - public SelectItems(IEnumerable items) + public Type EnumType { get; set; } + + public SelectType SelectType { get; set; } = SelectType.Dropdown; + + public IEnumerable GetItems(ModelExplorer explorer) + { + if (IsEnumItem()) + { + return GetItemsFromEnum(); + } + else + { + return GetItemsFromListField(explorer); + } + } + + private IEnumerable GetItemsFromListField(ModelExplorer explorer) + { + var properties = explorer.Properties.Where(p => p.Metadata.PropertyName.Equals(ItemsListPropertyName)).ToList(); + + return properties.Count > 0 + ? properties.First().Model as IEnumerable + : new List(); + } + + private IEnumerable 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; } } } diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectType.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectType.cs new file mode 100644 index 0000000000..b1c3bfa846 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/SelectType.cs @@ -0,0 +1,8 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form +{ + public enum SelectType + { + Dropdown, + Radio + } +} \ No newline at end of file