在TagHelpers中获取属性属性

Bee*_*ice 6 c# tag-helpers asp.net-core

一些模型属性具有"必需"数据注释,我需要在TagHelper类中读取.

public partial class Sale
{
    [Required]
    public string CustomerId { get; set; }
    ...
Run Code Online (Sandbox Code Playgroud)

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

<customer asp-for="CustomerId " value="@Model.CustomerId"></customer>
Run Code Online (Sandbox Code Playgroud)

在CustomerTagHelper类中有进程方法:

public override void Process(TagHelperContext context, TagHelperOutput output)
{
Run Code Online (Sandbox Code Playgroud)

如果当前绑定属性具有"required"属性,我怎么才能发现?我使用的是asp-net核心.

uns*_*Ptr 8

您可以通过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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Dan*_*.G. 7

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

@model WebApplication4.Models.Sale
...
<customer asp-for="CustomerId" />
Run Code Online (Sandbox Code Playgroud)

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

  • 适当的价值:source.Model
  • 物业名称:source.Name
  • 集装箱型号类型:source.Metadata.ContainerType
  • 是否必需标志: source.Metadata.IsRequired

您还将在 VS 中获得智能感知来为模型选择模型中的属性之一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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

public class Sale
{
    [Required]
    public string CustomerId { get; set; }
}
public class Promotion
{        
    public string CustomerId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这两个操作和视图中使用了哪些内容:

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" />
Run Code Online (Sandbox Code Playgroud)

将产生这些输出:

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
Run Code Online (Sandbox Code Playgroud)