Reputation: 13343
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?
Upvotes: 1
Views: 801
Reputation: 13343
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.
Upvotes: 3
Reputation: 6326
Using the binding context you can get a handle to the ValueProvider or the Request itself and pull the value out and parse it.
Upvotes: 0