Skip to content

Commit

Permalink
Code cleanup. Self-closing input tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bitmapped committed Feb 1, 2018
1 parent 02d36f1 commit 5461a42
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
28 changes: 13 additions & 15 deletions src/MvcEnumFlags/CheckBoxesForEnumFlagsModelHtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class CheckBoxesForEnumFlagsModelHtmlHelper
/// </summary>
/// <typeparam name="TModel">Type of flag enum</typeparam>
/// <typeparam name="TEnum"></typeparam>
/// <param name="htmlHelper">Html helper.</param>
/// <param name="htmlHelper">Html Helper</param>
/// <param name="expression">Item to generate checkboxes for</param>
/// <returns>Html code for checkboxes.</returns>
public static IHtmlString CheckBoxesForEnumFlagsFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
Expand All @@ -33,19 +33,16 @@ public static IHtmlString CheckBoxesForEnumFlagsFor<TModel, TEnum>(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 =
Expand All @@ -60,22 +57,23 @@ public static IHtmlString CheckBoxesForEnumFlagsFor<TModel, TEnum>(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);
}
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());

Expand Down
6 changes: 3 additions & 3 deletions src/MvcEnumFlags/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]

Expand All @@ -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")]

0 comments on commit 5461a42

Please sign in to comment.