diff --git a/src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs b/src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs index 9f688b7ccd2..65021d2b80c 100644 --- a/src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs +++ b/src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Xml.Serialization; -using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Nop.Web.Framework.Models { @@ -15,26 +13,16 @@ public partial record BaseNopModel /// /// Ctor /// - [Obsolete] public BaseNopModel() { - CustomProperties = new Dictionary(); + CustomProperties = new Dictionary(); PostInitialize(); } #endregion #region Methods - - /// - /// Perform additional actions for binding the model - /// - /// Model binding context - /// Developers can override this method in custom partial classes in order to add some custom model binding - public virtual void BindModel(ModelBindingContext bindingContext) - { - } - + /// /// Perform additional actions for the model initialization /// @@ -46,17 +34,12 @@ protected virtual void PostInitialize() #endregion #region Properties - - ////MVC is suppressing further validation if the IFormCollection is passed to a controller method. That's why we add it to the model - //[XmlIgnore] - //public IFormCollection Form { get; set; } - + /// /// Gets or sets property to store any custom values for models /// [XmlIgnore] - [Obsolete] - public Dictionary CustomProperties { get; set; } + public Dictionary CustomProperties { get; set; } #endregion diff --git a/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinder.cs b/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinder.cs index 70bc61463f9..4556f428f63 100644 --- a/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinder.cs +++ b/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinder.cs @@ -9,7 +9,6 @@ namespace Nop.Web.Framework.Mvc.ModelBinding.Binders /// /// Represents model binder for CustomProperties /// - [Obsolete] public class CustomPropertiesModelBinder : IModelBinder { Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext) @@ -19,40 +18,40 @@ Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext) var modelName = bindingContext.ModelName; - var result = new Dictionary(); + var result = new Dictionary(); if (bindingContext.HttpContext.Request.Method == "POST") { - var keys = bindingContext.HttpContext.Request.Form.Keys.ToList().Where(x => x.IndexOf(modelName) == 0); + var keys = bindingContext.HttpContext.Request.Form.Keys + .Where(x => x.IndexOf(modelName, StringComparison.Ordinal) == 0).ToList(); - if (keys != null && keys.Any()) + foreach (var key in keys) { - foreach (var key in keys) - { - var dicKey = key.Replace(modelName + "[", "").Replace("]", ""); - bindingContext.HttpContext.Request.Form.TryGetValue(key, out var value); - result.Add(dicKey, value.ToString()); - } + var dicKey = key.Replace(modelName + "[", "").Replace("]", ""); + bindingContext.HttpContext.Request.Form.TryGetValue(key, out var value); + result.Add(dicKey, value.ToString()); } } + if (bindingContext.HttpContext.Request.Method == "GET") { - var keys = bindingContext.HttpContext.Request.QueryString.Value.Split('&').Where(x => x.StartsWith(modelName)); - - if (keys != null && keys.Any()) + var queryStringValue = bindingContext.HttpContext.Request.QueryString.Value; + if (!string.IsNullOrEmpty(queryStringValue)) { + var keys = queryStringValue.TrimStart('?').Split('&').Where(x => x.StartsWith(modelName)).ToList(); + foreach (var key in keys) { - var dicKey = key[(key.IndexOf("[") + 1)..key.IndexOf("]")]; - var value = key[(key.IndexOf("=") + 1)..]; + var dicKey = key[(key.IndexOf("[", StringComparison.Ordinal) + 1)..key.IndexOf("]", StringComparison.Ordinal)]; + var value = key[(key.IndexOf("=", StringComparison.Ordinal) + 1)..]; result.Add(dicKey, value); } } } + bindingContext.Result = ModelBindingResult.Success(result); return Task.CompletedTask; - } } } diff --git a/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinderProvider.cs b/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinderProvider.cs index 1b4d1007709..6890605395d 100644 --- a/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinderProvider.cs +++ b/src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinderProvider.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.ModelBinding; using Nop.Web.Framework.Models; @@ -9,18 +7,14 @@ namespace Nop.Web.Framework.Mvc.ModelBinding.Binders /// /// Represents a model binder provider for CustomProperties /// - [Obsolete] public class CustomPropertiesModelBinderProvider : IModelBinderProvider { IModelBinder IModelBinderProvider.GetBinder(ModelBinderProviderContext context) { - var propertyBinders = context.Metadata.Properties - .ToDictionary(modelProperty => modelProperty, modelProperty => context.CreateBinder(modelProperty)); - - if (context.Metadata.ModelType == typeof(Dictionary) && context.Metadata.PropertyName == nameof(BaseNopModel.CustomProperties)) + if (context.Metadata.PropertyName == nameof(BaseNopModel.CustomProperties) && context.Metadata.ModelType == typeof(Dictionary)) return new CustomPropertiesModelBinder(); - else - return null; + + return null; } } }