I am working on a new project in .net Core having previously been working with .net Framework.
I wish to produce html select elements for boolean properties but using custom values instead of True and False (mainly "Yes" and "No"). In previous projects I have used the following method:
Create a custom attribute for boolean values:
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class BooleanCustomValuesAttribute : Attribute, IMetadataAware
{
public string TrueValue { get; set; }
public string FalseValue { get; set; }
public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.AdditionalValues["TrueValue"] = TrueValue;
metadata.AdditionalValues["FalseValue"] = FalseValue;
}
}
Use this in my model:
[BooleanCustomValues(TrueValue = "Yes", FalseValue = "No")]
public bool ThisWorks { get; set; }
Then in an editor template I can access these values for adding to a select:
object trueTitle;
ViewData.ModelMetadata.AdditionalValues.TryGetValue("TrueValue", out trueTitle);
trueTitle = trueTitle ?? "True";
object falseTitle;
ViewData.ModelMetadata.AdditionalValues.TryGetValue("FalseValue", out falseTitle);
falseTitle = falseTitle ?? "False";
I am now trying to do something similar in .net core, this time using a tag-helper instead of an editor template.
I understand that setting the AdditionalValues as above is not supported in core?
The closest I have found is this answer: Getting property attributes in TagHelpers
My tag helper code looks like this currenty (outputting p just for testing):
public class BooleanSelectTagHelper: TagHelper
{
[HtmlAttributeName("asp-for")]
public ModelExpression Source { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "p";
output.TagMode = TagMode.StartTagAndEndTag;
var booleanAttribute = Source.Metadata.ContainerType.GetProperty(Source.Name).GetCustomAttribute(typeof(BooleanCustomValuesAttribute));
var contents = $@"{booleanAttribute.TrueValue} or {booleanAttribute.FalseValue}";
output.Content.SetHtmlContent(new HtmlString(contents));
}
}
However I can't get it to work as Source.Metadata.ContainerType.GetProperty(Source.Name).GetCustomAttributes() returns null.
Anoyingly when debugging and looking at Source.Metadata in a watch window I can see exactly what I want to access under Source.Metadata.Attributes - however this is not something that seems to be exposed outside the debugger.
Apologies for length of post - any pointers would be much appreciated (including tellimg me I am doing this all wrong!)