Webiant Logo Webiant Logo
  1. No results found.

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

NopSelectTagHelper.cs

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Nop.Web.Framework.Extensions;

namespace Nop.Web.Framework.TagHelpers.Admin;

/// 
/// "nop-select" tag helper
/// 
[HtmlTargetElement("nop-select", TagStructure = TagStructure.WithoutEndTag)]
public partial class NopSelectTagHelper : TagHelper
{
    #region Constants

    protected const string FOR_ATTRIBUTE_NAME = "asp-for";
    protected const string NAME_ATTRIBUTE_NAME = "asp-for-name";
    protected const string ITEMS_ATTRIBUTE_NAME = "asp-items";
    protected const string MULTIPLE_ATTRIBUTE_NAME = "asp-multiple";
    protected const string REQUIRED_ATTRIBUTE_NAME = "asp-required";

    #endregion

    #region Fields

    protected readonly IHtmlHelper _htmlHelper;

    #endregion

    #region Ctor

    public NopSelectTagHelper(IHtmlHelper htmlHelper)
    {
        _htmlHelper = htmlHelper;
    }

    #endregion

    #region Methods

    /// 
    /// Asynchronously executes the tag helper with the given context and output
    /// 
    /// Contains information associated with the current HTML tag
    /// A stateful HTML element used to generate an HTML tag
    /// A task that represents the asynchronous operation
    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        ArgumentNullException.ThrowIfNull(context);

        ArgumentNullException.ThrowIfNull(output);

        //clear the output
        output.SuppressOutput();

        //required asterisk
        if (bool.TryParse(IsRequired, out var required) && required)
        {
            output.PreElement.SetHtmlContent("
"); output.PostElement.SetHtmlContent("
*
"); } //contextualize IHtmlHelper var viewContextAware = _htmlHelper as IViewContextAware; viewContextAware?.Contextualize(ViewContext); //get htmlAttributes object var htmlAttributes = new Dictionary(); var attributes = context.AllAttributes; foreach (var attribute in attributes) { if (!attribute.Name.Equals(FOR_ATTRIBUTE_NAME) && !attribute.Name.Equals(NAME_ATTRIBUTE_NAME) && !attribute.Name.Equals(ITEMS_ATTRIBUTE_NAME) && !attribute.Name.Equals(MULTIPLE_ATTRIBUTE_NAME) && !attribute.Name.Equals(REQUIRED_ATTRIBUTE_NAME)) { htmlAttributes.Add(attribute.Name, attribute.Value); } } //generate editor var tagName = For != null ? For.Name : Name; _ = bool.TryParse(IsMultiple, out var multiple); if (!string.IsNullOrEmpty(tagName)) { IHtmlContent selectList; if (multiple) { var templateName = For.ModelExplorer.ModelType == typeof(List) ? "MultiSelectString" : "MultiSelect"; selectList = _htmlHelper.Editor(tagName, templateName, new { htmlAttributes, SelectList = Items }); } else { if (htmlAttributes.ContainsKey("class")) htmlAttributes["class"] += " form-control"; else htmlAttributes.Add("class", "form-control"); selectList = _htmlHelper.DropDownList(tagName, Items, htmlAttributes); } output.Content.SetHtmlContent(await selectList.RenderHtmlContentAsync()); } } #endregion #region Properties /// /// An expression to be evaluated against the current model /// [HtmlAttributeName(FOR_ATTRIBUTE_NAME)] public ModelExpression For { get; set; } /// /// Name for a dropdown list /// [HtmlAttributeName(NAME_ATTRIBUTE_NAME)] public string Name { get; set; } /// /// Items for a dropdown list /// [HtmlAttributeName(ITEMS_ATTRIBUTE_NAME)] public IEnumerable Items { set; get; } = new List(); /// /// Indicates whether the field is required /// [HtmlAttributeName(REQUIRED_ATTRIBUTE_NAME)] public string IsRequired { set; get; } /// /// Indicates whether the input is multiple /// [HtmlAttributeName(MULTIPLE_ATTRIBUTE_NAME)] public string IsMultiple { set; get; } /// /// ViewContext /// [HtmlAttributeNotBound] [ViewContext] public ViewContext ViewContext { get; set; } #endregion }