title: Custom model binder for inner model
body: <p>I got a model like this:</p>
  
  <pre><code>public class MainModel
  {
     public string Id {get;set;}
     public string Title {get;set;}
     public TimePicker TimePickerField {get;set;}
  }
  </code></pre>
  
  <p><code>TimePicker</code> is an inner model which looks like this:</p>
  
  <pre><code>public class TimePicker 
  {
     public TimeSpan {get;set;}
     public AmPmEnum AmPm {get;set;}
  }
  </code></pre>
  
  <p>I'm trying to create a custom model binding for inner model: <code>TimePicker</code></p>
  
  <p>The question is: How do I get values in custom model binder which was submitted in form into <code>TimePicker</code> model fields?</p>
  
  <p>If I try to get it like this:</p>
  
  <pre><code>var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  </code></pre>
  
  <p>I just get <code>null</code> in <code>value</code>.</p>
  
  <p>I'm not sure how to implement the model binder correctly.</p>
  
  <pre><code>public class TimePickerModelBinder : DefaultModelBinder
  {
      public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
          if (bindingContext == null)
          {
              throw new ArgumentNullException("bindingContext");
          }
          var result = new TimePicker();
  
          var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
          if (value != null)
          {
              bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
              try
              {
                  //result = Duration.Parse(value.AttemptedValue);
              }
              catch (Exception ex)
              {
                 bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
              }
          }    
  
          return result;
      }
  }
  </code></pre>