Skip to content

Commit

Permalink
#6361 Fixed model bindind for BaseNopModel.CustomProperties[Obsolete]
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyKulagin committed Aug 12, 2022
1 parent d0c3653 commit 56a5eb9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Expand Up @@ -296,6 +296,7 @@ public static IMvcBuilder AddNopMvc(this IServiceCollection services)
{
//we'll use this until https://github.com/dotnet/aspnetcore/issues/6566 is solved
options.ModelBinderProviders.Insert(0, new InvariantNumberModelBinderProvider());
options.ModelBinderProviders.Insert(1, new CustomPropertiesModelBinderProvider());
//add custom display metadata provider
options.ModelMetadataDetailsProviders.Add(new NopMetadataProvider());
Expand Down
5 changes: 4 additions & 1 deletion src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc.ModelBinding;

Expand All @@ -14,6 +15,7 @@ public partial record BaseNopModel
/// <summary>
/// Ctor
/// </summary>
[Obsolete]
public BaseNopModel()
{
CustomProperties = new Dictionary<string, object>();
Expand Down Expand Up @@ -53,6 +55,7 @@ protected virtual void PostInitialize()
/// Gets or sets property to store any custom values for models
/// </summary>
[XmlIgnore]
[Obsolete]
public Dictionary<string, object> CustomProperties { get; set; }

#endregion
Expand Down
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Nop.Web.Framework.Mvc.ModelBinding.Binders
{
/// <summary>
/// Represents model binder for CustomProperties
/// </summary>
[Obsolete]
public class CustomPropertiesModelBinder : IModelBinder
{
Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));

var modelName = bindingContext.ModelName;

var result = new Dictionary<string, object>();
if (bindingContext.HttpContext.Request.Method == "POST")
{
var keys = bindingContext.HttpContext.Request.Form.Keys.ToList().Where(x => x.IndexOf(modelName) == 0);

if (keys != null && keys.Any())
{
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());
}
}
}
if (bindingContext.HttpContext.Request.Method == "GET")
{
var keys = bindingContext.HttpContext.Request.QueryString.Value.Split('&').Where(x => x.StartsWith(modelName));

if (keys != null && keys.Any())
{
foreach (var key in keys)
{
var dicKey = key[(key.IndexOf("[") + 1)..key.IndexOf("]")];
var value = key[(key.IndexOf("=") + 1)..];

result.Add(dicKey, value);
}
}
}
bindingContext.Result = ModelBindingResult.Success(result);

return Task.CompletedTask;

}
}
}
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Nop.Web.Framework.Models;

namespace Nop.Web.Framework.Mvc.ModelBinding.Binders
{
/// <summary>
/// Represents a model binder provider for CustomProperties
/// </summary>
[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<string, object>) && context.Metadata.PropertyName == nameof(BaseNopModel.CustomProperties))
return new CustomPropertiesModelBinder();
else
return null;
}
}
}

0 comments on commit 56a5eb9

Please sign in to comment.