Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bitmapped committed Feb 1, 2018
1 parent 5461a42 commit ce04e88
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/MvcEnumFlags/EnumFlagsModelBinder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcEnumFlags
Expand All @@ -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
{
Expand Down

0 comments on commit ce04e88

Please sign in to comment.