Jason Smith
Jason Smith

Reputation: 21

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?

Upvotes: 2

Views: 2409

Answers (3)

James Poulose
James Poulose

Reputation: 3833

I am not sure whether you got what you wanted. If you are looking to pass the complete model from view to the custom tag helper, this is how i do it.

You can pass in your current model from the view using any custom attributes. See the example below. Assuming your model is Country

public class Country
{
    public string Name { get; set; }
    public string Code { get; set; }

}

Now declare a property in your custom tag helper of the same type.

public Country CountryModel { get; set; }

Your controller would look something like this

public IActionResult Index()
{
    var country= new Country
    {
        Name = "United States",
        Code = "USA"
    };

    return View("Generic", country);
}

In this setup, to access your model inside the taghelper, just pass it in like any other custom attribute/property

Your view should now look like something like this

<country-select country-model="@Model"></country-select>

You can receive it in your tag helper like any other class property

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

Happy coding!

Upvotes: 0

Denis Taran
Denis Taran

Reputation: 715

In order to get property name, you should use ModelExpression in your class instead:

public class CountrySelectTagHelper : TagHelper
{
    public ModelExpression For { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var propertyName = For.Metadata.PropertyName;
        var value = For.Model as string;

        ...

        base.Process(context, output);
    }
}

Upvotes: 6

Ralf B&#246;nning
Ralf B&#246;nning

Reputation: 15425

You can pass a string via the tag helper attribute.

 <country-select value="@Model.CountryCode"  />

The 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.

Upvotes: 1

Related Questions