using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sarand.Core.Base; namespace Sarand.OperationsMgr.Web.System { //======================================================================= /// /// Maps a browser request to a data object. Default class provides a base implementation of a model binder. /// Overridden to trim whitespace from input values. /// public class OpnModelBinder : IModelBinder { //------------------------------------------------------------------- // Construction. public OpnModelBinder () { } //------------------------------------------------------------------- public Task BindModelAsync (ModelBindingContext bindingContext) { // Ignore selected properties by name. var modelName = bindingContext.ModelName; if (modelName == "RetainAllWhiteSpace") return Task.CompletedTask; ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(modelName); if (valueProviderResult == ValueProviderResult.None) return Task.CompletedTask; // Trim text values. bindingContext.Result = ModelBindingResult.Success(valueProviderResult.FirstValue.TrimAll()); return Task.CompletedTask; } //------------------------------------------------------------------- } //======================================================================= }