Skip to content
This repository was archived by the owner on Nov 21, 2022. It is now read-only.

Modelbinding

Lodewijk Sioen edited this page Feb 14, 2015 · 4 revisions

Shared Modelbinding

Unlike Routing, Modelbinding is not shared between Webforms and Mvc. For Mvc, you can find all the classes and interfaces in the System.Web.Mvc namespace and for Webforms they live in System.Web.Modelbinding. At first glance they look and operate the same, but they are two completly separate implementations.

Modelvalidation

ExitStrategy provides all the plumbing to share the validation of your models between the two worlds. You can have a Razor view with inline validation on a Webforms page where a ValidationSummary Control uses the same data to provide a summary. You can try this on our demo site.

IModelbinder

Both namespaces contain an IModelBinder interface (Webforms|Mvc). Their signature differ and they need to be registered in diffrent ways:

System.Web.Mvc.ModelBinders.Binders.Add(typeof(DateTime), new MvcDateTimeModelBinder());
System.Web.ModelBinding.ModelBinderProviders.Providers.RegisterBinderForType(typeof(DateTime), new WebformsDateTimeModelBinder());

ExitStrategy will help you bridge the two parallel implementations. There is a IModelBinder interface that combines the two. Classes that implement this interface can be registered to Webforms and Mvc using the ModelBinderAdapter:

 ModelBinderAdapter.AddModelBinder(typeof(DateTime), new SharedDateTimeModelBinder());
Clone this wiki locally