Get property name in Tag Helper
NickName:Jason Smith Ask DateTime:2016-08-29T15:43:47

Get property name in Tag Helper

ASP.NET Core introduced custom tag helpers which can be used in views like this:

<country-select value="CountryCode"  />

However, I don't understand how can I get model property name in my classes:

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

In the previous version I was able to do this by using ModelMetadata:

var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var property = metadata.PropertyName; // return "CountryCode"

How can I do the same in the new ASP.NET tag helpers?

Copyright Notice:Content Author:「Jason Smith」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39200960/get-property-name-in-tag-helper

Answers
Denys Krasnoshchok 2016-08-29T18:31:46

In order to get property name, you should use ModelExpression in your class instead:\n\npublic class CountrySelectTagHelper : TagHelper\n{\n public ModelExpression For { get; set; }\n\n public override void Process(TagHelperContext context, TagHelperOutput output)\n {\n var propertyName = For.Metadata.PropertyName;\n var value = For.Model as string;\n\n ...\n\n base.Process(context, output);\n }\n}\n",


Ralf Bönning 2016-08-29T08:16:37

You can pass a string via the tag helper attribute.\n\n <country-select value=\"@Model.CountryCode\" />\n\n\nThe Value property will be populated by Razor with the value of Model.CountryCode by prepending @. So you get the value directly without the need to pass the name of a model property and accessing that afterwards.",


More about “Get property name in Tag Helper” related questions

Get property name in Tag Helper

ASP.NET Core introduced custom tag helpers which can be used in views like this: &lt;country-select value="CountryCode" /&gt; However, I don't understand how can I get model property name in my

Show Detail

Get name of the View containing Tag Helper

I have a custom Tag Helper which needs to know the name or filename of the View that contains the Tag Helper. Is this possible?

Show Detail

.NET Core Tag Helper - Delay rendering of tag helpers in content of another tag helper when calling GetChildContentAsync

I am creating a tag helper to render a table using an IEnumerable as the data source. I am using tokens with the property name in the data object that the tag helper will replace with the actual va...

Show Detail

Tag helper `Attributes` alias

I couldn't find any way to give multiple names to a single tag helper (alias) in netcore20 to the following tag helper class definition [HtmlTargetElement(Attributes = AttributeName)] public class

Show Detail

Pass ModelExpression from a Tag Helper to a Partial View

I'm using the example found here to allow the rendering of a partial view from within a tag helper. What I'm going for here is to be able to define a tag helper like so: &lt;mydateinput for="@Model.

Show Detail

MVC Helper Object and Property name

I made my first MVC helper to split long strings in a table and I also validate if string is NULL and user can send NullString ='NA' to show instead of empty string. public static IHtmlString Spl...

Show Detail

From within the tag helper, how can I access the attributes that are not properties of the tag helper?

If I have the following tag helper: using Microsoft.AspNetCore.Razor.TagHelpers; namespace CustomTagHelper.TagHelpers { [HtmlTargetElement("my-first-tag-helper")] public class

Show Detail

Helper is wrapped in a script tag

This one must be very easy to answer, though I'm stuck... ember 1.0.0 handlebars 1.0.0 I defined the following (simplified) helper to generate a url: Ember.Handlebars.helper('imageUrl', function(

Show Detail

Select tag helper - unexpected behavior when controller argument name matches model property

I ran into an issue with the default value of a &lt;select&gt; element not being properly set to the value of the property model I set it to in the tag helper. My original code that led to the issue:

Show Detail

.NET Tag Helper to replicate @Html.DisplayFor

I'm discovering .Net Core Tag Helpers and I was just curious to know if there are any tag helpers that replicate the @Html.DisplayFor. I think that the label tag helper replicates @Html.DisplayNa...

Show Detail