Net Core TagHelpers:具有仅接受某些参数的属性

时间:2019-07-01 18:45:00

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

我想创建一个自定义标记帮助器,该方法只接受某些按钮类型的值。 ButtonType只能是“ Radio”或“ Checkbox”等。 尝试添加枚举时,它在下面给出错误。可以选择执行此操作吗?除非需要,否则不要使用if语句条件字符串。

    [HtmlTargetElement("TagHelperTest")]
    public class ABCDTest:TagHelper
    {
        [HtmlAttributeName("HeaderTitle")]
        public string HeaderTitle { get; set; }

        [HtmlAttributeName("TextDescription")]
        public string TextDescription { get; set; }

        [HtmlAttributeName("ButtonTypetests")]
        public enum ButtonType { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            string ReturnText = "Title: " + HeaderTitle + 
                                "Description" + HeaderDescription + 
                                "Button Type" + ButtonType;

        output.Content.SetHtmlContent(ReturnText);
        }

测试:

    [HtmlAttributeName("ButtonType")]
    public enum ButtonType { get; set; }

错误:

Attribute 'HtmlAttributeName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.

查看.cshtml理想目标

<ABCDTest
    HeaderTitle = "Test"
    TextDescription= "Test description"
    ButtonType = "Radio">
</ABCDTest>

0 个答案:

没有答案