Archive for August 2010
Asp.net MVC 2: Creating a SimpleValuesModelBinder
I have been thinking about what it would take to make a custom model binder for the PayPal IPN service the past week or so, and tonight I finally had the chance to look into it. The goals is simple – I just want to pass the IPN form values to a model that has properties with different names. I don’t want to do any custom model binding, perhaps a small bit of string parsing but that’s about it.
I looked into model binding at the MSDN library, Steve Sanderson’s book, and the TekPub MVC series, they all were good, but what I wanted to do was simple. On thing I did get out of it was that I would need both a custom ModelBinder implementing IModelBinder and possibly a custom ValuesProvider as well.
Finally, I looked into the MVC source for the DefaultModelBinder to see how it worked and came up with pure gold. Here is the basic premise of the solution:
- Create an abstract model binder class that uses the DefaultModelBinder class to do its work.
- Leverage the NameValueCollectionValueProvider class to provide simple Key/Value pairs to the DefaultModelBinder .
- Create a custom copy of the supplied ModelBindingContext that connects the binder and provider together.
Here’s the code with comments:
////all complicated binding set up is abstracted
//DefaultModelBinder is used for ModelBinding
//NameValueCollectionValueProvider is used as the ValueProvider
public abstract class SimpleValuesModelBinder : IModelBinder
{
protected abstract void AppendValues(ControllerContext controllerContext, NameValueCollection collection);
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//first we want to call AppendValues
//the collection is supplied to the SimpleValuesProvider
//we use the NameValueCollectionValueProvider because it uses key/value pairs
var collection = new NameValueCollection();
AppendValues(controllerContext, collection);
//create a custom binding context
//we pass thru properties except the value provider
//we use the new TestValueProvider here
var customContext = new ModelBindingContext()
{
ModelMetadata = bindingContext.ModelMetadata,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = new NameValueCollectionValueProvider(collection, CultureInfo.CurrentCulture)
};
//pass our custom binding context the default binding model and bind
return new DefaultModelBinder().BindModel(controllerContext, customContext);
}
}
Creating a custom model binder from this class is very simple. Here is a simple class:
{
public string Value { get; set; }
public DateTime Current { get; set; }
}
… and here is custom binder using the abstract class above:
{
protected override void AppendValues(ControllerContext controllerContext, NameValueCollection collection)
{
collection["Value"] = Guid.NewGuid().ToString();
collection["Current"] = DateTime.Now.ToString();
}
}
The resulting custom model binder is very simple. All you have to do is override one method, AppendValues, and your done. In this case I am supplying arbitrary values as a test, but you can leverage any values you want from the ControllerContext parameter.
Of course set up is easy too, ASP.net MVC allows several ways to do this. For a test, I’ll just apply to a parameter in an action method.
{
return Content(string.Format("Value: {0}; Current: {1}",Test.Value,Test.Current));
}
And the the less than glamorous result:
So, if your looking to do some simple value parsing this may help.
IE 9 Preview 4 – How much HTML 5 support? Should it?
I’ve been wondering how to get thru the hype of Html 5 support in IE 9. That’s not to say that the improvements they have made aren’t welcome – they very much are. But Html 5 is larger spec than just audio, video and canvas – and I’ve been wondering how broad Html 5 support goes.
I found a site called http://html5test.com/ that will test your browser for HTML 5 support. How does IE 9 preview 4 fare?
That’s right – 96 out of 300? You say you want details? Here you go:
- Parsing rules1 Bonus point1/11
<!DOCTYPE html>triggers standards mode
Yes ✔HTML5 tokenizer
No ✘HTML5 tree building
No ✘HTML5 defines rules for embedding SVG and MathML inside a regular HTML document. Support for SVG and MathML is not required though, so bonus points are awarded if your browser supports embedding these two technologies.
SVG in
text/html
Yes ✔MathML in
text/html
No ✘ - Canvas20
canvaselement
Yes ✔2D context
Yes ✔Text
Yes ✔ - Video22/27
videoelement
Yes ✔Subtitle support
No ✘Poster image support
Yes ✔The following tests go beyond the requirements of the HTML5 specification and are not counted towards the total score. If a browser supports one or more video codecs, two bonus points are awarded for each codec.
MPEG-4 support
No ✘H.264 support
No ✘Ogg Theora support
No ✘WebM support
No ✘ - Audio2 Bonus points20
audioelement
Yes ✔The following tests go beyond the requirements of the HTML5 specification and are not counted towards the total score. If a browser supports one or more audio codecs, one bonus point is awarded for each codec.
PCM audio support
No ✘MP3 support
Yes ✔AAC support
Yes ✔Ogg Vorbis support
No ✘WebM support
No ✘ - Local devices0/20
deviceelement
No ✘ - Elements13/30
Embedding custom non-visible data
No ✘Section elements
No ✘sectionelement
No ✘navelement
No ✘articleelement
No ✘asideelement
No ✘hgroupelement
No ✘headerelement
No ✘footerelement
No ✘Grouping content elements
No ✘figureelement
No ✘figcaptionelement
No ✘Text-level semantic elements
2 out of 5markelement
No ✘rubyelement
Yes ✔rtelement
Yes ✔rpelement
No ✘timeelement
No ✘hiddenattribute
No ✘Scroll into view
Yes ✔ - Forms0/38
inputelement types
No ✘searchtype state
No ✘teltype state
No ✘urltype state
No ✘emailtype state
No ✘datetimetype state
No ✘datetype state
No ✘monthtype state
No ✘weektype state
No ✘timetype state
No ✘datetime-localtype state
No ✘numbertype state
No ✘rangetype state
No ✘colortype state
No ✘inputelement attributes
No ✘autofocusattribute
No ✘listattribute
No ✘maxattribute
No ✘minattribute
No ✘multipleattribute
No ✘patternattribute
No ✘requiredattribute
No ✘stepattribute
No ✘Other form elements
No ✘datalistelement
No ✘keygenelement
No ✘outputelement
No ✘progresselement
No ✘meterelement
No ✘Form validation
No ✘ - User interaction5/25
Drag and drop
No ✘Undo history
No ✘Session history
No ✘Text selection
Yes ✔ - Microdata0/10
Microdata
No ✘ - Web applications0/14
Application Cache
No ✘
Related specifications
- Geolocation0/10
Geolocation
No ✘ - WebGL0/10
3D context
No ✘ - Communication5/25
Cross-document messaging
Yes ✔WebSocket
No ✘Server-Sent Events
No ✘ - Files0/10
FileReader API
No ✘ - Storage10/20
Session Storage
Yes ✔Local Storage
Yes ✔IndexedDB
No ✘The Web SQL Database specification is no longer being updated and has been replaced by IndexedDB. Because at least 3 vendors have shipped implementations of this specification we still include it in this test.
Web SQL Database
No ✘ - Workers0/10
Web Workers
No ✘
Clearly, while the IE team has done great work in bring IE forward with the IE 9 previews, clearly there is a lot more HTML left unsupported then currently in the latest preview. But should IE 9 have full HTML 5 support? Good question. If you take a look at the HTML 5 document at the W3C you’ll notice it’s “working draft” – the proposal in still flux. I would guess that MS is just expanding “de facto” standards support and hitting the highlights, where other features such as IndexDB, Web Applications, Html 5 Forms and other elements will come – eventually. But will they come in IE 9? Only MS knows the answer to that one. Let’s just hope we don’t have to wait three years until IE 10 to find out.