Best Practices when Implementing IModelBinder [closed]
Asked Answered
W

4

28

I'm looking for a set of best practices to use when implementing IModelBinder.

I've read three different MVC books and each one does some slightly different stuff in their implementations without any real explanation.

  • The Hanselman, Haack, Guthrie, Conery book doesn't even mention IModelBinder
  • Palermo recommends extending DefaultModelBinder rather than direct implementation of IModelBinder, but I don't really see how to leverage the benefits
  • Sanderson mentions updating existing Model instances, as well as calling ModelState.SetModelValue() to follow convention.

I just want to make sure that my model binders are following conventions, and that I properly understand the entire ModelBindingContext.

Any tips, tricks, GOOD tutorials to recommend?

Watson answered 11/10, 2009 at 11:51 Comment(0)
M
10

K Scott Allen has some tips about Model Binding: http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx.

Miraculous answered 11/10, 2009 at 12:15 Comment(1)
The article mentions Data Annotations so I wondered if I should inherit from DataAnnotationsModelBinder instead. The answer is no. MVC 2 RTM's DefaultModelBinder already contains logic from DataAnnotationsModelBinder. See #2667455Dialectics
H
2

I inherited from DefaultModelBinder, because it automatically binds basic properties in entity. I enhanced it, so it binds also navigation properties. My binder performs inherited binding first and then searches for additional, navigation property values in form. I think that Your approach should depend on what You really want to do. You can also use reflector and see what really stands behind default binder. This may convince You to inherit.

Hardej answered 11/10, 2009 at 13:44 Comment(0)
W
2

Travis Illig recently wrote a really good blog article about data type validation when creating a custom model binder.

I've never seen this advice before and I thought it was really good - so I wanted to contribute this link: http://www.paraesthesia.com/archive/2012/01/31/data-type-validation-and-model-binding-in-asp-net-mvc.aspx

Watson answered 25/2, 2012 at 6:49 Comment(0)
D
0

MVC Futures 2 has an extensible model binding system. You can find out more in the release documentation at http://aspnet.codeplex.com/releases/view/41742

Just download "ASP.NET MVC 2 Futures Extensible Model Binder Documentation"

Dialectics answered 10/8, 2010 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.