Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

StringModelBinder.cs

using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;

namespace Nop.Web.Framework.Mvc.ModelBinding.Binders;

/// 
/// Represents model binder for string properties
/// 
public partial class StringModelBinder : IModelBinder
{
    Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext)
    {
        ArgumentNullException.ThrowIfNull(bindingContext);

        var valueProviderResult =
            bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (valueProviderResult.FirstValue is { } str &&
            !string.IsNullOrEmpty(str))
        {
            var noTrim = (bindingContext.ModelMetadata as DefaultModelMetadata)?.Attributes.Attributes.Any(p => p.GetType() == typeof(NoTrimAttribute));

            bindingContext.Result = ModelBindingResult.Success((noTrim.HasValue && noTrim.Value) ? str : str.Trim());
        }

        return Task.CompletedTask;
    }
}