diff --git a/src/Orchard/Mvc/ModelBinders/BooleanBinderProvider.cs b/src/Orchard/Mvc/ModelBinders/BooleanBinderProvider.cs new file mode 100644 index 000000000..50cae3434 --- /dev/null +++ b/src/Orchard/Mvc/ModelBinders/BooleanBinderProvider.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Web.Mvc; + +namespace Orchard.Mvc.ModelBinders { + public class BooleanBinderProvider : IModelBinderProvider, IModelBinder { + + public IEnumerable GetModelBinders() { + return new[] { + new ModelBinderDescriptor { + ModelBinder = this, + Type = typeof(bool) + } + }; + } + + public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { + var value = false; + try { + value = Convert.ToBoolean(bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue); + } catch { + bindingContext.ModelState.AddModelError(bindingContext.ModelName, new FormatException()); + } + return value; + } + } +} diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index c9f494184..60e9f9128 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -182,6 +182,7 @@ +