Skip to content

Commit

Permalink
Revert "Removed the BaseNopModel.CustomProperties property"
Browse files Browse the repository at this point in the history
This reverts commit bea6cbb.
  • Loading branch information
skoshelev committed Dec 20, 2022
1 parent 75fcd00 commit bd4e309
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public MapperConfiguration()
CreateMap<FacebookPixelConfiguration, FacebookPixelModel>()
.ForMember(model => model.AvailableStores, options => options.Ignore())
.ForMember(model => model.CustomEventSearchModel, options => options.Ignore())
.ForMember(model => model.CustomProperties, options => options.Ignore())
.ForMember(model => model.HideCustomEventsSearch, options => options.Ignore())
.ForMember(model => model.HideStoresList, options => options.Ignore())
.ForMember(model => model.StoreName, options => options.Ignore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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
36 changes: 34 additions & 2 deletions src/Presentation/Nop.Web.Framework/Models/BaseNopModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Nop.Web.Framework.Models
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Nop.Web.Framework.Models
{
/// <summary>
/// Represents base nopCommerce model
Expand All @@ -10,15 +15,26 @@ public partial record BaseNopModel
/// <summary>
/// Ctor
/// </summary>
[Obsolete]
public BaseNopModel()
{
CustomProperties = new Dictionary<string, object>();
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 @@ -28,5 +44,21 @@ 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; }

#endregion

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

}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ public AdminMapperConfiguration()
//add some generic mapping rules
this.Internal().ForAllMaps((mapConfiguration, map) =>
{
//exclude Form and CustomProperties from mapping BaseNopModel
if (typeof(BaseNopModel).IsAssignableFrom(mapConfiguration.DestinationType))
{
//map.ForMember(nameof(BaseNopModel.Form), options => options.Ignore());
map.ForMember(nameof(BaseNopModel.CustomProperties), options => options.Ignore());
}

//exclude ActiveStoreScopeConfiguration from mapping ISettingsModel
if (typeof(ISettingsModel).IsAssignableFrom(mapConfiguration.DestinationType))
map.ForMember(nameof(ISettingsModel.ActiveStoreScopeConfiguration), options => options.Ignore());
Expand Down

0 comments on commit bd4e309

Please sign in to comment.