在TagHelpers中获取属性属性 [英] Getting property attributes in TagHelpers

查看:251
本文介绍了在TagHelpers中获取属性属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些模型属性具有"Required"数据注释,我需要在TagHelper类中进行读取.

Some of model properties has "Required" data annotation, that I need to read in a TagHelper class.

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

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

In the sales view I create a custom select for customer:

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

在CustomerTagHelper类中,存在处理方法:

And in the CustomerTagHelper class there is the process method:

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

在这一点上我如何发现当前的绑定属性是否具有"required"属性?我正在使用asp-net核心.

How can I discover at this point, if the current bind property has the "required" attribute? I´m using asp-net core.

推荐答案

标签帮助程序除了提供您作为其属性输入的内容外,不知道其他任何事情.因此,您想要创建一个可以按如下方式使用的标签助手:

The tag helper doesn't know about anything other than what you provide as input for its attributes. So you want to create a tag helper that you can use as follows:

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

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

Then you would declare a property of type ModelSource associated with the asp-for attribute. That would give you access to not just the value of the property but also metadata like the following (and more!):

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

您还将在VS中获得智能感知,为 asp-for 模型选择模型中的一个属性,如果该值不是模型属性的名称,则会引发错误.

You will also get intellisense in VS to select one of properties in your model for the asp-for model and it will throw an error if the value isnt the name of a model property.

作为一个例子,看看这个标签助手:

As an example, take a look at this tag helper:

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

然后,如果您具有以下两种型号:

Then if you had these 2 models:

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

在以下两个操作和视图中使用了哪些:

Which are used in these 2 actions and views:

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

这篇关于在TagHelpers中获取属性属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆