diff --git a/src/Presentation/Nop.Web.Framework/Mvc/IModelAttribute.cs b/src/Presentation/Nop.Web.Framework/Mvc/IModelAttribute.cs index 5986fc5fe6d..d795592c205 100644 --- a/src/Presentation/Nop.Web.Framework/Mvc/IModelAttribute.cs +++ b/src/Presentation/Nop.Web.Framework/Mvc/IModelAttribute.cs @@ -1,7 +1,14 @@ -namespace Nop.Web.Framework.Mvc + +namespace Nop.Web.Framework.Mvc { + /// + /// Represents custom model attribute + /// public interface IModelAttribute { + /// + /// Gets name of the attribute + /// string Name { get; } } } diff --git a/src/Presentation/Nop.Web.Framework/Mvc/NopMetadataProvider.cs b/src/Presentation/Nop.Web.Framework/Mvc/NopMetadataProvider.cs index 72b1ffe6fd6..35762df4aa7 100644 --- a/src/Presentation/Nop.Web.Framework/Mvc/NopMetadataProvider.cs +++ b/src/Presentation/Nop.Web.Framework/Mvc/NopMetadataProvider.cs @@ -1,32 +1,31 @@ -#if NET451 -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Mvc; +using System.Linq; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Nop.Core; namespace Nop.Web.Framework.Mvc { /// - /// This MetadataProvider adds some functionality on top of the default DataAnnotationsModelMetadataProvider. - /// It adds custom attributes (implementing IModelAttribute) to the AdditionalValues property of the model's metadata - /// so that it can be retrieved later. + /// Represents metadata provider that adds custom attributes to the model's metadata, so it can be retrieved later /// - public class NopMetadataProvider : DataAnnotationsModelMetadataProvider + public class NopMetadataProvider : IDisplayMetadataProvider { - protected override ModelMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName) + /// + /// Sets the values for properties of isplay metadata + /// + /// Display metadata provider context + public void CreateDisplayMetadata(DisplayMetadataProviderContext context) { - var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); - var additionalValues = attributes.OfType().ToList(); + //get all custom attributes + var additionalValues = context.Attributes.OfType().ToList(); + + //and try add them as additional values of metadata foreach (var additionalValue in additionalValues) { - if (metadata.AdditionalValues.ContainsKey(additionalValue.Name)) - throw new NopException("There is already an attribute with the name of \"" + additionalValue.Name + - "\" on this model."); - metadata.AdditionalValues.Add(additionalValue.Name, additionalValue); + if (context.DisplayMetadata.AdditionalValues.ContainsKey(additionalValue.Name)) + throw new NopException("There is already an attribute with the name '{0}' on this model", additionalValue.Name); + + context.DisplayMetadata.AdditionalValues.Add(additionalValue.Name, additionalValue); } - return metadata; } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Presentation/Nop.Web.Framework/NopResourceDisplayName.cs b/src/Presentation/Nop.Web.Framework/NopResourceDisplayName.cs index 27c3446845a..7b539a83109 100644 --- a/src/Presentation/Nop.Web.Framework/NopResourceDisplayName.cs +++ b/src/Presentation/Nop.Web.Framework/NopResourceDisplayName.cs @@ -1,43 +1,67 @@ -using Nop.Core; +using System.ComponentModel; +using Nop.Core; using Nop.Core.Infrastructure; using Nop.Services.Localization; using Nop.Web.Framework.Mvc; namespace Nop.Web.Framework { - public class NopResourceDisplayName : System.ComponentModel.DisplayNameAttribute, IModelAttribute + /// + /// Represents model attribute that specifies the display name by passed key of the locale resource + /// + public class NopResourceDisplayName : DisplayNameAttribute, IModelAttribute { + #region Fields + private string _resourceValue = string.Empty; - //private bool _resourceValueRetrived; - public NopResourceDisplayName(string resourceKey) - : base(resourceKey) + #endregion + + #region Ctor + + /// + /// Create instance of the attribute + /// + /// Key of the locale resource + public NopResourceDisplayName(string resourceKey) : base(resourceKey) { ResourceKey = resourceKey; } + #endregion + + #region Properties + + /// + /// Gets or sets key of the locale resource + /// public string ResourceKey { get; set; } + /// + /// Getss the display name + /// public override string DisplayName { get { - //do not cache resources because it causes issues when you have multiple languages - //if (!_resourceValueRetrived) - //{ - var langId = EngineContext.Current.Resolve().WorkingLanguage.Id; - _resourceValue = EngineContext.Current - .Resolve() - .GetResource(ResourceKey, langId, true, ResourceKey); - // _resourceValueRetrived = true; - //} + //get working language identifier + var workingLanguageId = EngineContext.Current.Resolve().WorkingLanguage.Id; + + //get locale resource value + _resourceValue = EngineContext.Current.Resolve().GetResource(ResourceKey, workingLanguageId, true, ResourceKey); + return _resourceValue; } } + /// + /// Gets name of the attribute + /// public string Name { - get { return "NopResourceDisplayName"; } + get { return nameof(NopResourceDisplayName); } } + + #endregion } } diff --git a/src/Presentation/Nop.Web/Startup.cs b/src/Presentation/Nop.Web/Startup.cs index 86f064fee80..9e9a7812c5c 100644 --- a/src/Presentation/Nop.Web/Startup.cs +++ b/src/Presentation/Nop.Web/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Nop.Core.Extensions; +using Nop.Web.Framework.Mvc; using Nop.Web.Framework.Mvc.Extensions; using Nop.Web.Framework.Mvc.Routes; @@ -41,7 +42,7 @@ public Startup(IHostingEnvironment environment) public IServiceProvider ConfigureServices(IServiceCollection services) { //add MVC feature - services.AddMvc(); + services.AddMvc().AddMvcOptions(options => options.ModelMetadataDetailsProviders.Add(new NopMetadataProvider())); //add MiniProfiler services services.AddMiniProfiler();