title: .NET MVC C# Enum binder
body: <p>I successfully made Enum binder and from what iv tested, everything works. But i want to have nullable enum parameter in my controller and when i change to nullable, my binder dont work.</p>
  
  <pre><code> public class EnumBinder&lt;T&gt; : IModelBinder
      {
          private object DefaultValue { get; set; }
          public EnumBinder(object defaultValue)
          {
              DefaultValue = defaultValue;
          }
  
  
          public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  
              if(result == null || string.IsNullOrEmpty(result.AttemptedValue))
              {
                  return DefaultValue;
              }
  
              return GetEnumValue&lt;T&gt;(result.AttemptedValue);
          }
  
  
  
          public static object GetEnumValue&lt;T&gt;(string value)
          {
              var enums = Enum.GetValues(typeof (T)).Cast&lt;T&gt;().ToList();
  
              foreach (var e in enums)
              {
                  var _enum = Enum.Parse(typeof (T), e.ToString());
                  if (string.Equals(value, _enum.ToString(), StringComparison.InvariantCultureIgnoreCase))
                  {
                      return _enum;
                  }
              }
  
              return null;
          }
  
      }
  </code></pre>
  
  <p>Works on</p>
  
  <pre><code> public virtual ActionResult ControllerName(Status status)
      {
         return View();
      }
  </code></pre>
  
  <p>but dont work on </p>
  
  <pre><code> public virtual ActionResult ControllerName(Status? status)
      {
         return View();
      }
  </code></pre>
  
  <p>Of course i have registered my binder on application start</p>
  
  <pre><code>ModelBinders.Binders.Add(typeof(CallStatus), new EnumBinder&lt;CallStatus&gt;(null));
  </code></pre>
  
  <p>What i do wrong, why i cant return nullable enum in my controller?</p>