在TagHelpers中获取属性属性

时间:2016-11-09 12:18:15

标签: c# asp.net-core tag-helpers

某些模型属性具有“必需”数据注释,我需要在TagHelper类中读取。

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

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

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

在CustomerTagHelper类中有进程方法:

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

如果当前绑定属性具有“required”属性,我怎么能发现?我使用的是asp-net core。

3 个答案:

答案 0 :(得分:3)

标记帮助程序除了提供的属性输入之外,不知道任何其他内容。因此,您需要创建一个可以使用的标记帮助程序,如下所示:

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

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

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

您还将在VS中获取intellisense,为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

答案 1 :(得分:3)

您可以通过 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>中使用它。

答案 2 :(得分:0)

你可以这样做:

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