From ce04e88e200dfb1e6e5c441de2c2f874f92450dd Mon Sep 17 00:00:00 2001 From: Bitmapped Date: Thu, 1 Feb 2018 11:09:54 -0500 Subject: [PATCH] Code cleanup --- src/MvcEnumFlags/EnumFlagsModelBinder.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/MvcEnumFlags/EnumFlagsModelBinder.cs b/src/MvcEnumFlags/EnumFlagsModelBinder.cs index d479300..d1216e5 100644 --- a/src/MvcEnumFlags/EnumFlagsModelBinder.cs +++ b/src/MvcEnumFlags/EnumFlagsModelBinder.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Web.Mvc; namespace MvcEnumFlags @@ -15,19 +12,16 @@ public override object BindModel(ControllerContext controllerContext, ModelBindi if (value != null) { // Get type of value. - Type valueType = bindingContext.ModelType; + var valueType = bindingContext.ModelType; - var rawValues = value.RawValue as string[]; - if (rawValues != null) + // Try to parse enum values if present + if (value.RawValue is string[] rawValues) { - // Create instance of result object. - var result = (Enum)Activator.CreateInstance(valueType); - + // Try to parse variable. try { // Parse. - result = (Enum)Enum.Parse(valueType, string.Join(",", rawValues)); - return result; + return (Enum)Enum.Parse(valueType, string.Join(",", rawValues)); } catch {