From a9301c922005edd38a0ba6d1ca680d24c33a6542 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 13 May 2019 12:21:08 +0300 Subject: [PATCH] Dynamic forms: nullable enum --- .../Form/AbpSelectTagHelperService.cs | 18 ++++++++++++++---- .../Pages/Components/DynamicForms.cshtml.cs | 4 ++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs index b07100497e..46ac3a8e90 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs @@ -187,13 +187,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form { var localizer = _tagHelperLocalizer.GetLocalizer(explorer); - var selectItems = explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) + var selectItems = new List(); + var isNullableType = Nullable.GetUnderlyingType(explorer.ModelType) != null; + var enumType = explorer.ModelType; + + if (isNullableType) + { + enumType = Nullable.GetUnderlyingType(explorer.ModelType); + selectItems.Add(new SelectListItem()); + } + + selectItems.AddRange(enumType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) .Select((t, i) => new SelectListItem { - Value = ((int) Enum.Parse(explorer.ModelType, t.Name)).ToString(), - Text = GetLocalizedPropertyName(localizer, explorer.ModelType, t.Name) - }).ToList(); + Value = ((int) Enum.Parse(enumType, t.Name)).ToString(), + Text = GetLocalizedPropertyName(localizer, enumType, t.Name) + }).ToList()); return selectItems; } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/Pages/Components/DynamicForms.cshtml.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/Pages/Components/DynamicForms.cshtml.cs index 276f760a27..adfd91c688 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/Pages/Components/DynamicForms.cshtml.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo/Pages/Components/DynamicForms.cshtml.cs @@ -123,6 +123,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.Demo.Pages.Components [Display(Name = "My Car Type")] public CarType MyCarType { get; set; } + [Required] + [Display(Name = "My Car Type Nullable")] + public CarType? MyCarTypeNullable { get; set; } + [Required] [AbpRadioButton(Inline = true)] [Display(Name = "Your Car Type")]