小编Jas*_*ith的帖子

在标签助手中获取属性名称

ASP.NET Core引入了自定义标记帮助器,可以在如下视图中使用:

<country-select value="CountryCode"  />
Run Code Online (Sandbox Code Playgroud)

但是,我不明白如何在类中获取模型属性名称:

public class CountrySelectTagHelper : TagHelper
{
    [HtmlAttributeName("value")]
    public string Value { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
       ...
       // Should return property name, which is "CountryCode" in the above example
       var propertyName = ???();  
       base.Process(context, output);
    }
}
Run Code Online (Sandbox Code Playgroud)

在以前的版本中,我可以使用ModelMetadata以下命令执行此操作:

var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var property = metadata.PropertyName; // return "CountryCode"
Run Code Online (Sandbox Code Playgroud)

如何在新的ASP.NET标签助手中执行相同操作?

asp.net-mvc razor tag-helpers asp.net-core

2
推荐指数
1
解决办法
1414
查看次数

标签 统计

asp.net-core ×1

asp.net-mvc ×1

razor ×1

tag-helpers ×1