From 5fb3840550022c9975706b87a47b803906483762 Mon Sep 17 00:00:00 2001 From: yekalkan Date: Thu, 24 May 2018 07:48:55 +0300 Subject: [PATCH] Select tag helper refactoring --- .../Form/AbpDynamicformTagHelperService.cs | 4 +-- .../Form/AbpSelectTagHelperService.cs | 33 ++++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs index e7e46667b5..9337fad1ba 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs @@ -152,10 +152,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form { return IsRadioGroup(model.ModelExplorer) ? GetAbpRadioInputTagHelper(model) : - GetSelectGroupTagHelper(model); + GetSelectTagHelper(model); } - private AbpTagHelper GetSelectGroupTagHelper(ModelExpression model) + private AbpTagHelper GetSelectTagHelper(ModelExpression model) { var abpSelectTagHelper = _serviceProvider.GetRequiredService(); abpSelectTagHelper.AspFor = model; diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs index 25a04ee20f..ec23c5e754 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs @@ -93,12 +93,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput selectTag) { - if (string.IsNullOrEmpty(TagHelper.Label)) + if (!string.IsNullOrEmpty(TagHelper.Label)) { - return GetLabelAsHtmlUsingTagHelper(context, output); + return ""; } - return ""; + return GetLabelAsHtmlUsingTagHelper(context, output); } protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List selectItems) @@ -118,10 +118,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form protected virtual void SetSelectedValue(TagHelperContext context, TagHelperOutput output, List selectItems) { - var selectedValue = GetSelectedValue(context, output); - if (!selectItems.Any(si => si.Selected)) { + var selectedValue = GetSelectedValue(context, output); + var itemToBeSelected = selectItems.FirstOrDefault(si => si.Value.ToString() == selectedValue); if (itemToBeSelected != null) @@ -133,20 +133,21 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form protected virtual string GetSelectedValue(TagHelperContext context, TagHelperOutput output) { - if (TagHelper.AspFor.ModelExplorer.Metadata.IsEnum) + var modelExplorer = TagHelper.AspFor.ModelExplorer; + + if (modelExplorer.Metadata.IsEnum) { - var baseType = TagHelper.AspFor.ModelExplorer.Model?.GetType().GetEnumUnderlyingType(); + var baseType = modelExplorer.Model?.GetType().GetEnumUnderlyingType(); - if (baseType == null) - { - return null; - } + if (baseType == null) { return null; } - var valueAsString = Convert.ChangeType(TagHelper.AspFor.ModelExplorer.Model, baseType); - return valueAsString != null ? valueAsString.ToString() : ""; + var value = Convert.ChangeType(modelExplorer.Model, baseType); + return value != null ? value.ToString() : ""; + } + else + { + return modelExplorer.Model?.ToString(); } - - return TagHelper.AspFor.ModelExplorer.Model?.ToString(); } protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output) @@ -156,7 +157,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form For = TagHelper.AspFor, ViewContext = TagHelper.ViewContext }; - + return RenderTagHelper(new TagHelperAttributeList(), context, labelTagHelper, _encoder, "span", TagMode.StartTagAndEndTag, true); } }