Skip to content

Instantly share code, notes, and snippets.

@scottaddie
Created March 3, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottaddie/024fa1e3d47cb4cbb0d094a3077d2094 to your computer and use it in GitHub Desktop.
Save scottaddie/024fa1e3d47cb4cbb0d094a3077d2094 to your computer and use it in GitHub Desktop.
Custom Tag Helper for rendering a LinkButton-like anchor in ASP.NET Core
[HtmlTargetElement("link-button")]
public class LinkButtonTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
const string HIDDEN_FIELD_ID = "form_marker";
output.TagName = "a";
output.Attributes.SetAttribute("onclick",
$@"document.getElementById(""{HIDDEN_FIELD_ID}"").form.submit();");
output.PostElement.SetHtmlContent($@"<input type=""hidden"" id=""{HIDDEN_FIELD_ID}"" />");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment