Webiant Logo Webiant Logo
  1. No results found.

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

InputTagHelper.cs

?using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

 namespace Nop.Web.Framework.TagHelpers.Public; 

 /// 
 /// "input" tag helper
 /// 
 [HtmlTargetElement("input", Attributes = FOR_ATTRIBUTE_NAME)]
 public partial class InputTagHelper : Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper
 {
     #region Constants

     protected const string FOR_ATTRIBUTE_NAME = "asp-for";
     protected const string DISABLED_ATTRIBUTE_NAME = "asp-disabled";

     #endregion

     #region Ctor

     public InputTagHelper(IHtmlGenerator generator) : base(generator)
     {
     }

     #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);

         //add disabled attribute
         if (bool.TryParse(IsDisabled, out var disabled) && disabled)
             output.Attributes.Add(new TagHelperAttribute("disabled", "disabled"));

         try
         {
             await base.ProcessAsync(context, output);
         }
         catch
         {
             //If the passed values differ in data type according to the model, we should try to initialize the component with a default value. 
             //If this is not possible, then we suppress the generation of html for this imput.
             try
             {
                 ViewContext.ModelState[For.Name].RawValue = Activator.CreateInstance(For.ModelExplorer.ModelType);
                 await base.ProcessAsync(context, output);
             }
             catch
             {
                 output.SuppressOutput();
             }
         }
     }

     #endregion

     #region Properties

     /// 
     /// Indicates whether the input is disabled
     /// 
     [HtmlAttributeName(DISABLED_ATTRIBUTE_NAME)]
     public string IsDisabled { set; get; }

     #endregion
 }