I'm new with Asp.Net Core and I'm having problems with Tag Helpers.
I'm trying to create a Tag Helper for 2 scenarios: - Input group - Checkbox
When I start from an asp-for tag I have no problem building other tags, but I don't know how to build "intelligent" tags such as an input tag with the asp-for field and all the validations.
Here is what I'm trying to build:
INPUT GROUP
<div class="input-group">
<input class="form-control" placeholder="Search" type="text" data-val="true" data-val-maxlength="Max value" data-val-maxlength-max="5" id="SearchText" name="SearchText" value="" maxlength="5">
<span class="input-group-append"><button class="btn btn-light" id="myButtonId" type="button">Search</button></span>
</div>
And this is what I have now:
<div class="input-group">
<input asp-for="SearchText" class="form-control" placeholder="Buscar" add-on-button="myButtonId" add-on-append="true" />
</div>
And this is the Tag Helper
[HtmlTargetElement("input", Attributes = "add-on-button,add-on-append", ParentTag = "div")]
public class TextBoxButtonHelper : TagHelper
{
public string AddOnButton { get; set; }
public bool AddOnAppend { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var tagAddOn = new TagBuilder("span");
tagAddOn.AddCssClass(AddOnAppend ? "input-group-append" : "input-group-prepend");
var tagButton = new TagBuilder("button");
tagButton.Attributes.Add("id", AddOnButton);
tagButton.Attributes.Add("class", "btn btn-light");
tagButton.Attributes.Add("type", "button");
tagButton.InnerHtml.Append(context.AllAttributes["placeholder"]?.Value.ToString());
tagAddOn.InnerHtml.AppendHtml(tagButton);
if (AddOnAppend)
output.PostElement.AppendHtml(tagAddOn);
else
output.PreElement.AppendHtml(tagAddOn);
}
}
What I'd like to have would be something like this:
<input-tag asp-for="SearchText" class="form-control" placeholder="Buscar" button="myButtonId" append="true" />
So just one tag that would generate all the code, but I don't know how to generate the "TextBoxFor" inside the Tag Helper.
CHECKBOX The same problem with the chechbox, I need to create this from just one tag:
<div class="custom-control custom-control-primary custom-checkbox">
<input class="custom-control-input" data-val="true" data-val-required="The SearchActive field is required." id="SearchActive" name="SearchActive" type="checkbox" value="true">
<label class="custom-control-label" for="SearchActive">Solo activos</label>
<input name="SearchActive" type="hidden" value="false">
</div>
Thanks for helping...