Skip to content

Commit

Permalink
Refactoring of CustomProperties binder
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshelev committed Dec 20, 2022
1 parent bd4e309 commit dcfbf41
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
27 changes: 5 additions & 22 deletions src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -15,26 +13,16 @@ public partial record BaseNopModel
/// <summary>
/// Ctor
/// </summary>
[Obsolete]
public BaseNopModel()
{
CustomProperties = new Dictionary<string, object>();
CustomProperties = new Dictionary<string, string>();
PostInitialize();
}

#endregion

#region Methods

/// <summary>
/// Perform additional actions for binding the model
/// </summary>
/// <param name="bindingContext">Model binding context</param>
/// <remarks>Developers can override this method in custom partial classes in order to add some custom model binding</remarks>
public virtual void BindModel(ModelBindingContext bindingContext)
{
}


/// <summary>
/// Perform additional actions for the model initialization
/// </summary>
Expand All @@ -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; }


/// <summary>
/// Gets or sets property to store any custom values for models
/// </summary>
[XmlIgnore]
[Obsolete]
public Dictionary<string, object> CustomProperties { get; set; }
public Dictionary<string, string> CustomProperties { get; set; }

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Nop.Web.Framework.Mvc.ModelBinding.Binders
/// <summary>
/// Represents model binder for CustomProperties
/// </summary>
[Obsolete]
public class CustomPropertiesModelBinder : IModelBinder
{
Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext)
Expand All @@ -19,40 +18,40 @@ Task IModelBinder.BindModelAsync(ModelBindingContext bindingContext)

var modelName = bindingContext.ModelName;

var result = new Dictionary<string, object>();
var result = new Dictionary<string, string>();
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;

}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,18 +7,14 @@ 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))
if (context.Metadata.PropertyName == nameof(BaseNopModel.CustomProperties) && context.Metadata.ModelType == typeof(Dictionary<string, string>))
return new CustomPropertiesModelBinder();
else
return null;

return null;
}
}
}

0 comments on commit dcfbf41

Please sign in to comment.