-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Removed the BaseNopModel.CustomProperties property"
This reverts commit bea6cbb.
- Loading branch information
Showing
6 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Presentation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ntation/Nop.Web.Framework/Mvc/ModelBinding/Binders/CustomPropertiesModelBinderProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters