MVC2 IModelBinder and parsing a string to an object - How do I do it?

Posted by burnt_hand on Stack Overflow See other posts from Stack Overflow or by burnt_hand
Published on 2010-02-02T23:50:26Z Indexed on 2010/04/03 6:33 UTC
Read the original article Hit count: 283

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET