title: .NET MVC 3 Custom Decimal? Model Binder
body: <p>In my model, I have the following decimal? property:</p>
  
  <pre><code>public decimal? Budget { get; set; }
  </code></pre>
  
  <p>I realize that I need a custom model binder for decimals which Haack provided at the following link: <a href="http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx" rel="noreferrer">http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx</a>.</p>
  
  <p>I have modified his code so that if the value being passed in does contain a currency symbol and/or comma then I strip it and then try to convert it to a decimal. This works but being that my property is a nullable decimal type, I also want it to accept nothing and move along its merry way inserting a null into that column in my database. Right now it inserts a 0.00. I know I'm missing something in my code but am having a brain freeze.</p>
  
  <p>Here's the binder code:</p>
  
  <pre><code> public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  {
      ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
      ModelState modelState = new ModelState { Value = valueResult };
      object actualValue = null;
      object newValue = null;
      try
      {
          if (!string.IsNullOrEmpty(valueResult.AttemptedValue))
          {
              newValue = valueResult.AttemptedValue.Replace("$", "").Replace(",", "");
          }
  
          actualValue = Convert.ToDecimal(newValue, CultureInfo.CurrentCulture);
      }
      catch (FormatException e)
      {
          modelState.Errors.Add(e);
      }
  
      bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
      return actualValue;
  }
  </code></pre>
  
  <p>Again, the goal is to have decimal? act like it usually does but if there is a value which contains a currency symbol and/or comma and it can be converted to a decimal then to return that.</p>
  
  <p>Thanks</p>