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标签助手中执行相同操作?