在 TagHelpers 中获取 属性 个属性

Getting property attributes in TagHelpers

一些模型属性有 "Required" 数据注释,我需要在 TagHelper 中读取 class。

public partial class Sale
{
    [Required]
    public string CustomerId { get; set; }
    ...

在销售视图中,我为客户创建了一个自定义 select:

<customer asp-for="CustomerId " value="@Model.CustomerId"></customer>

并且在 CustomerTagHelper class 中有处理方法:

public override void Process(TagHelperContext context, TagHelperOutput output)
{

此时我如何发现当前绑定 属性 是否具有 "required" 属性?我正在使用 asp-net 核心。

你可以这样做:

    var type = typeof(YOUR_CLASS);
    var property = type.GetProperty("FIELD_NAME");
    var isRequired = Attribute.IsDefined(property, typeof(Required));

除了您为其属性提供的输入之外,标签助手不知道任何其他信息。所以你想创建一个标签助手,你可以按如下方式使用:

@model WebApplication4.Models.Sale
...
<customer asp-for="CustomerId" />

然后您将声明一个 ModelSource 类型的 属性 与 asp-for 属性相关联。这将使您不仅可以访问 属性 的值,还可以访问如下元数据(以及更多!):

  • 属性 值:source.Model
  • 属性 姓名:source.Name
  • 容器模型类型:source.Metadata.ContainerType
  • IsRequired 标志: source.Metadata.IsRequired

您还将在 VS 中获得 select 模型中 asp-for 模型的一个属性的智能感知,如果该值不是模型名称,它将抛出错误 属性.


举个例子,看看这个标签助手:

public class CustomerTagHelper: TagHelper
{
    [HtmlAttributeName("asp-for")]
    public ModelExpression Source { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "p";
        output.TagMode = TagMode.StartTagAndEndTag;

        var contents = $@"
            Model name: {Source.Metadata.ContainerType.FullName}<br/>
            Property name: {Source.Name}<br/>
            Current Value: {Source.Model}<br/> 
            Is Required: {Source.Metadata.IsRequired}";

        output.Content.SetHtmlContent(new HtmlString(contents));
    }
}

那么如果你有这两个模型:

public class Sale
{
    [Required]
    public string CustomerId { get; set; }
}
public class Promotion
{        
    public string CustomerId { get; set; }
}

这 2 个操作和视图中使用了哪些:

public IActionResult Sale()
{
    return View();
}

@model WebApplication4.Models.Sale
...
<customer asp-for="CustomerId" />


public IActionResult Promotion()
{
    return View(new Models.Promotion { CustomerId = "abc-123" });
}

@model WebApplication4.Models.Promotion
...
<customer asp-for="CustomerId" />

将产生这些输出:

Tag helper for: WebApplication4.Models.Sale
Property name: CustomerId
Current Value: 
Is Required: True

Model name: WebApplication4.Models.Promotion
Property name: CustomerId
Current Value: abc-123
Is Required: False

您可以通过 ModelExpression.

访问自定义属性
public class CustomTagHelper : TagHelper
{
    [HtmlAttributeName("asp-for")]
    public ModelExpression For { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {            
        CustomAttribute attribute = For.Metadata
                                       .ContainerType
                                       .GetProperty(For.Name)
                                       .GetCustomAttribute(typeof(CustomAttribute)) 
                                       as CustomAttribute;
        if (attribute != null)
        {
            output.Attributes.Add("some-attr", attribute.Text);
        }
    }
}

然后在您的模板中使用它 <custom asp-for="SomeProp"></custom>