Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

NopMetadataProvider.cs

using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Nop.Core;

namespace Nop.Web.Framework.Mvc.ModelBinding;

/// 
/// Represents metadata provider that adds custom attributes to the model's metadata, so it can be retrieved later
/// 
public partial class NopMetadataProvider : IDisplayMetadataProvider
{
    /// 
    /// Sets the values for properties of isplay metadata
    /// 
    /// Display metadata provider context
    public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
    {
        //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 (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);
        }
    }
}