Search code examples
asp.net-corerazorasp.net-core-mvcabp-frameworkasp.net-core-tag-helpers

Empty item as HTML <option> for abp-select


In Razor pages, you can specify an empty first item for a select by doing something similar to this:

<select asp-for="SelectedTags" asp-items="Model.TagOptions">
    <option value="">Choose a tag</option>
</select>

Is there an option for the abp-select Tag Helper such as:

<abp-select asp-for="Mentor.RegionId" asp-items="Model.Regions">
    <option value="">Choose a region</option>
</abp-select>

Reference: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-5.0#the-select-tag-helper


Solution

  • It is not possible as of ABP 4.3.0, because AbpSelectTagHelperService modifies output.Content without calling and using the result of output.GetChildContentAsync.

    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    {
        var innerHtml = await GetFormInputGroupAsHtmlAsync(context, output);
    
        ...
    
        if (suppress)
        {
            output.SuppressOutput();
        }
        else
        {
            output.TagName = "div";
            ...
            output.Content.SetHtmlContent(innerHtml);
        }
    }
    
    protected virtual async Task<string> GetFormInputGroupAsHtmlAsync(TagHelperContext context, TagHelperOutput output)
    {
        var selectTag = await GetSelectTagAsync(context, output);
        ...
    }
    
    protected virtual async Task<TagHelperOutput> GetSelectTagAsync(TagHelperContext context, TagHelperOutput output)
    {
        ...
    
        var selectTagHelperOutput = await selectTagHelper.ProcessAndGetOutputAsync(GetInputAttributes(context, output), context, "select", TagMode.StartTagAndEndTag);
    
        // selectTagHelperOutput.Content.SetHtmlContent(childContent); // Missing
        ...
    }
    

    References: