<small id='tcmRE'></small><noframes id='tcmRE'>

  • <tfoot id='tcmRE'></tfoot>
    • <bdo id='tcmRE'></bdo><ul id='tcmRE'></ul>
    1. <i id='tcmRE'><tr id='tcmRE'><dt id='tcmRE'><q id='tcmRE'><span id='tcmRE'><b id='tcmRE'><form id='tcmRE'><ins id='tcmRE'></ins><ul id='tcmRE'></ul><sub id='tcmRE'></sub></form><legend id='tcmRE'></legend><bdo id='tcmRE'><pre id='tcmRE'><center id='tcmRE'></center></pre></bdo></b><th id='tcmRE'></th></span></q></dt></tr></i><div id='tcmRE'><tfoot id='tcmRE'></tfoot><dl id='tcmRE'><fieldset id='tcmRE'></fieldset></dl></div>

      <legend id='tcmRE'><style id='tcmRE'><dir id='tcmRE'><q id='tcmRE'></q></dir></style></legend>

      1. TagHelpers 基于验证属性为 LabelTagHelper 添加自定义

        时间:2023-08-08

            <bdo id='fJ9eG'></bdo><ul id='fJ9eG'></ul>
            1. <legend id='fJ9eG'><style id='fJ9eG'><dir id='fJ9eG'><q id='fJ9eG'></q></dir></style></legend>
                <tbody id='fJ9eG'></tbody>
              <i id='fJ9eG'><tr id='fJ9eG'><dt id='fJ9eG'><q id='fJ9eG'><span id='fJ9eG'><b id='fJ9eG'><form id='fJ9eG'><ins id='fJ9eG'></ins><ul id='fJ9eG'></ul><sub id='fJ9eG'></sub></form><legend id='fJ9eG'></legend><bdo id='fJ9eG'><pre id='fJ9eG'><center id='fJ9eG'></center></pre></bdo></b><th id='fJ9eG'></th></span></q></dt></tr></i><div id='fJ9eG'><tfoot id='fJ9eG'></tfoot><dl id='fJ9eG'><fieldset id='fJ9eG'></fieldset></dl></div>

                <small id='fJ9eG'></small><noframes id='fJ9eG'>

                <tfoot id='fJ9eG'></tfoot>

                • 本文介绍了TagHelpers 基于验证属性为 LabelTagHelper 添加自定义类 [必需]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 Core MVC 中,有一个新概念,即标签助手.

                  In Core MVC there is anew concept as Tag helpers.

                  我们之前可以创建自定义 html 帮助器,以根据验证数据注释附加一些类,例如 [Required].

                  We could previously create custom html helpers to attach some classes based on the validation data annotations such as [Required].

                  作为 TagHelpers arq 相当新的领域,我找不到足够的资源来实现以下目标:

                  As TagHelpers arq quite new area I cannot find anough resources to achieve the following:

                  这是视图模型:

                      [Required]
                      public Gender Gender { get; set; }
                  

                  查看:

                  <label class="control-label col-md-3 required" asp-for="Gender"></label>
                  

                  css:

                  .required:after {
                  content: "*";
                  font-weight: bold;
                  color: red;
                  }
                  

                  输出:

                  但我不想在标签中手动添加所需的 css 类.不知何故,我应该能够扩展 LabelTagHelper 以读取模型数据注释,如果它具有 [Required] 然后在标签元素中添加所需的类.

                  But I don't want to manully add the required css class in the label. Somehow I shoudl be able to extend the LabelTagHelper to read model data annotations and if it has the [Required] then add required class in the label element.

                  谢谢,

                  推荐答案

                  是的,您可以通过从 LabelTagHelper 类继承并首先将您自己的类添加到属性列表中来轻松扩展它.

                  Yup, you can extend this pretty easily by inheriting from the LabelTagHelper class and adding in your own class to the attribute list first.

                  [HtmlTargetElement("label", Attributes = "asp-for")]
                  public class RequiredLabelTagHelper : LabelTagHelper
                  {
                      public RequiredLabelTagHelper(IHtmlGenerator generator) : base(generator)
                      {
                      }
                  
                      public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
                      {
                          if (For.Metadata.IsRequired)
                          {
                              CreateOrMergeAttribute("class", "required", output);
                          }
                  
                          return base.ProcessAsync(context, output);
                      }
                  
                      private void CreateOrMergeAttribute(string name, object content, TagHelperOutput output)
                      {
                          var currentAttribute = output.Attributes.FirstOrDefault(attribute => attribute.Name == name);
                          if (currentAttribute == null)
                          {
                              var attribute = new TagHelperAttribute(name, content);
                              output.Attributes.Add(attribute);
                          }
                          else
                          {
                              var newAttribute = new TagHelperAttribute(
                                  name,
                                  $"{currentAttribute.Value.ToString()} {content.ToString()}",
                                  currentAttribute.ValueStyle);
                              output.Attributes.Remove(currentAttribute);
                              output.Attributes.Add(newAttribute);
                          }
                      }
                  }
                  

                  这篇关于TagHelpers 基于验证属性为 LabelTagHelper 添加自定义类 [必需]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Tag Helpers - 自关闭 HTML 标签是个坏习惯? 下一篇:使用内置功能在 MVC6 中使用 JQuery AJAX 提交剃刀表

                  相关文章

                  最新文章

                    <tfoot id='msByf'></tfoot>
                    <i id='msByf'><tr id='msByf'><dt id='msByf'><q id='msByf'><span id='msByf'><b id='msByf'><form id='msByf'><ins id='msByf'></ins><ul id='msByf'></ul><sub id='msByf'></sub></form><legend id='msByf'></legend><bdo id='msByf'><pre id='msByf'><center id='msByf'></center></pre></bdo></b><th id='msByf'></th></span></q></dt></tr></i><div id='msByf'><tfoot id='msByf'></tfoot><dl id='msByf'><fieldset id='msByf'></fieldset></dl></div>
                      <bdo id='msByf'></bdo><ul id='msByf'></ul>

                    <small id='msByf'></small><noframes id='msByf'>

                  1. <legend id='msByf'><style id='msByf'><dir id='msByf'><q id='msByf'></q></dir></style></legend>