views:

280

answers:

2

I have an object called Time

public class Time{
   public int Hour  {get;set;}
   public int Minute {get;set;}     

   public static Time Parse(string timeString){
   //reads the ToString()'s previous output and returns a Time object
   }

   override protected string ToString(){
   //puts out something like 14:50 (as in 2:50PM)
   }
}

So what I want is for the automatic model binding on the Edit or Create action to set this Time instance up from a string (i.e. feed the Parse method with the string and return the result).

The reason I am doing this is that I will have a DropDownList with selectable times. The value of each option will be the parser readable string.

Can anyone provide an example BindModel method from the IModelBinder interface?

A: 

Using the binding context you can get a handle to the ValueProvider or the Request itself and pull the value out and parse it.

Maxwell Troy Milton King
+3  A: 

OK, I found what I wanted. Model Binding by Scott Hanselman You are a legend Scott.

The key component was using the "bindingContext.ModelName" to get the name of the key and then pull the value from the value provider like so:

bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue

Of course that's a fairly naive implementation, but it got me started.

burnt_hand