Dear All;

We are using nopcommerce 4.2 version. And i noticed nop team have some custom data field for customproperty of objects. But oldest version (for example 3.9) we can use that property for our custom pages at the plugins for post data. But with the net core versions it have binding problem from the ui when data posted.
As my understanding net core binder have problem bind data to Dictionary<string,object>.

So i fix that with writing my own model binder and i inject it to startup. Here is my codes. What you think about that? Maybe nopcommerce team fix that at the new version with NopModelBinder.

Thanks.


    public class CustomPropertiesModelBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            var propertyBinders = context.Metadata.Properties
                    .ToDictionary(modelProperty => modelProperty, modelProperty => context.CreateBinder(modelProperty));

            if (context.Metadata.ModelType == typeof(Dictionary<string, object>) && context.Metadata.PropertyName == "CustomProperties") return new CustomPropertiesModelBinder();
            else return null;
        }
    }



    public class CustomPropertiesModelBinder : IModelBinder
    {

        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
                throw new ArgumentNullException(nameof(bindingContext));

            var modelName = bindingContext.ModelName;

            var result = new Dictionary<string, object>();

            var keys = bindingContext.HttpContext.Request.Form.Keys.ToList().Where(x=>x.IndexOf(modelName) == 0);

            if(keys != null && keys.Count() > 0)
            {
                foreach(var key in keys)
                {
                    string dicKey = key.Replace(modelName + "[", "").Replace("]", "");
                    bindingContext.HttpContext.Request.Form.TryGetValue(key, out var value);
                    result.Add(dicKey, value.ToArray());
                }
            }

            bindingContext.Result = ModelBindingResult.Success(result);

            return Task.CompletedTask;
        
        }
    }


And Startup.cs :


        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {

            services.AddMvc(option => option.ModelBinderProviders.Insert(1, new CustomPropertiesModelBinderProvider()));
        }