From 5461a42d2f2aa7094e2479a23b195298f7fccecc Mon Sep 17 00:00:00 2001 From: Bitmapped Date: Thu, 1 Feb 2018 10:44:40 -0500 Subject: [PATCH] Code cleanup. Self-closing input tag. --- .../CheckBoxesForEnumFlagsModelHtmlHelper.cs | 28 +++++++++---------- src/MvcEnumFlags/Properties/AssemblyInfo.cs | 6 ++-- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/MvcEnumFlags/CheckBoxesForEnumFlagsModelHtmlHelper.cs b/src/MvcEnumFlags/CheckBoxesForEnumFlagsModelHtmlHelper.cs index 3c8701a..564a1f3 100644 --- a/src/MvcEnumFlags/CheckBoxesForEnumFlagsModelHtmlHelper.cs +++ b/src/MvcEnumFlags/CheckBoxesForEnumFlagsModelHtmlHelper.cs @@ -17,7 +17,7 @@ public static class CheckBoxesForEnumFlagsModelHtmlHelper /// /// Type of flag enum /// - /// Html helper. + /// Html Helper /// Item to generate checkboxes for /// Html code for checkboxes. public static IHtmlString CheckBoxesForEnumFlagsFor(this HtmlHelper htmlHelper, Expression> expression) @@ -33,19 +33,16 @@ public static IHtmlString CheckBoxesForEnumFlagsFor(this HtmlHelp throw new ArgumentException("This helper can only be used with enums. Type used was: " + enumModelType.FullName + "."); } - // Create string for Element. + // Create HTML string for the item. var sb = new StringBuilder(); foreach (Enum item in Enum.GetValues(enumModelType)) { if (Convert.ToInt64(item) != 0) { - var ti = htmlHelper.ViewData.TemplateInfo; - var id = fullHtmlFieldName + ti.GetFullHtmlFieldId(item.ToString()); - var label = new TagBuilder("label"); - label.Attributes["for"] = id; - var field = item.GetType().GetField(item.ToString()); + var templateInfo = htmlHelper.ViewData.TemplateInfo; + var id = $"{fullHtmlFieldName}_{templateInfo.GetFullHtmlFieldId(item.ToString())}"; - // Add checkbox. + // Create checkbox. var checkbox = new TagBuilder("input") { Attributes = @@ -60,12 +57,14 @@ public static IHtmlString CheckBoxesForEnumFlagsFor(this HtmlHelp { checkbox.Attributes["checked"] = "checked"; } - sb.AppendLine(checkbox.ToString()); + sb.AppendLine(checkbox.ToString(TagRenderMode.SelfClosing)); + + // Create label. + var label = new TagBuilder("label") { Attributes = { ["for"] = id } }; + var field = item.GetType().GetField(item.ToString()); // Check to see if DisplayName attribute has been set for item. - var displayName = field.GetCustomAttributes(typeof(DisplayNameAttribute), true) - .FirstOrDefault() as DisplayNameAttribute; - if (displayName != null) + if (field.GetCustomAttributes(typeof(DisplayNameAttribute), true).FirstOrDefault() is DisplayNameAttribute displayName) { // Display name specified. Use it. label.SetInnerText(displayName.DisplayName); @@ -73,9 +72,8 @@ public static IHtmlString CheckBoxesForEnumFlagsFor(this HtmlHelp else { // Check to see if Display attribute has been set for item. - var display = field.GetCustomAttributes(typeof(DisplayAttribute), true) - .FirstOrDefault() as DisplayAttribute; - label.SetInnerText((display != null) ? display.Name : item.ToString()); + var displayString = field.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() is DisplayAttribute display ? display.Name : item.ToString(); + label.SetInnerText(displayString); } sb.AppendLine(label.ToString()); diff --git a/src/MvcEnumFlags/Properties/AssemblyInfo.cs b/src/MvcEnumFlags/Properties/AssemblyInfo.cs index a584497..6e1b1e5 100644 --- a/src/MvcEnumFlags/Properties/AssemblyInfo.cs +++ b/src/MvcEnumFlags/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Brian M. Powell")] [assembly: AssemblyProduct("MvcEnumFlags")] -[assembly: AssemblyCopyright("Copyright ©2017, Brian M. Powell.")] +[assembly: AssemblyCopyright("Copyright ©2018, Brian M. Powell.")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.1.0")] -[assembly: AssemblyFileVersion("1.1.1.0")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")]