I really love the new tag helpers in .NET Core but I want to do something slightly different.
By default you can use <select asp-items=""> to make an old fashioned select list with options.
But I'd like to add a data-image-url attribute to each option so I can show a picture.
<select>
<option value="1" data-image-url="/picture.jpg">First Option</option>
<option value="2" data-image-url="/another-picture.jpg">Second Option</option>
</select>
I created a new class that inherits from SelectListItem:
public class ProductSelectListItem : SelectListItem
{
public string ImageUrl { get; set; }
}
but I'm having trouble figuring out how to inherit from the standard tag helper class.
Of course I could just put a for loop in the view but I'd like to see if there is a better way.
Is it possible to inherit from the standard Select tag helper class? An editor template is also an option but I don't know if it is the best/cleanest way to go in .NET Core.
I think we have to use custom
taghelperhere. And actually I didn't find a solution to inherit defaultselecttaghelper, so I create a new one.Related document: create tag helper, the select tag helper.
Firstly, I prepared a new class like
SelectListItem. I copied the code fromSelectListItembut added new properitydata_image_url:Then create custom taghelper
MselectTagHelper:Create a viewmodel:
Then add
@addTagHelper *, projectNamein_ViewImports.cshtml.In the view: