Fixes Boolean Conversion error (#8393)

This commit is contained in:
Hermes Sbicego
2020-09-03 19:07:47 +02:00
committed by GitHub
parent 868ce12ad6
commit c566e12abd
2 changed files with 27 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace Orchard.Mvc.ModelBinders {
public class BooleanBinderProvider : IModelBinderProvider, IModelBinder {
public IEnumerable<ModelBinderDescriptor> GetModelBinders() {
return new[] {
new ModelBinderDescriptor {
ModelBinder = this,
Type = typeof(bool)
}
};
}
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
var value = false;
var requestBooleanValue = controllerContext.HttpContext.Request[bindingContext.ModelName].Split(',')[0]; //Html.CheckBox and Html.CheckBoxFor return "true,false" string
if (!bool.TryParse(requestBooleanValue, out value)) {
bindingContext.ModelState.AddModelError(bindingContext.ModelName, new FormatException());
}
return value;
}
}
}

View File

@@ -182,6 +182,7 @@
<Compile Include="Data\Migration\Schema\DropUniqueConstraintCommand.cs" />
<Compile Include="Environment\Extensions\Models\LifecycleStatus.cs" />
<Compile Include="Environment\ShellBuilders\ICompositionStrategy.cs" />
<Compile Include="Mvc\ModelBinders\BooleanBinderProvider.cs" />
<Compile Include="Mvc\Updater.cs" />
<Compile Include="Recipes\Models\ConfigurationContext.cs" />
<Compile Include="Recipes\Models\RecipeBuilderStepConfigurationContext.cs" />