From b528ecd1e0c2b6169f8756240bdbae38207d588c Mon Sep 17 00:00:00 2001 From: Matteo Piovanelli Date: Fri, 11 Sep 2020 11:02:53 +0200 Subject: [PATCH] New version of Boolean Binder Provider (#8413) * New version of Boolean Binder Provider * Use Convert.ToBoolean(string) rather than ValueProviderResult.ConvertTo(bool) --- .../Mvc/ModelBinders/BooleanBinderProvider.cs | 27 +++++++++++++++++++ src/Orchard/Orchard.Framework.csproj | 1 + 2 files changed, 28 insertions(+) create mode 100644 src/Orchard/Mvc/ModelBinders/BooleanBinderProvider.cs 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 @@ +