Search code examples
asp.net-mvchelperactionlink

Convert an HTML button to ActionLink


I have an html button setup and functioning with a set range of required properties that I'd like to convert to a text-based link instead. Additionally, so I can familiarize myself with working html helpers and intellesense I'd like to see how to shoehorn these properties into an ActionLink:

<input type="button" id="RemoveRegistration_Submit<%=row.ID %>" 
value="Remove From Cart" 
onclick="$('#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();" 
align="right" />

thx


Solution

  • It shouldn't be that hard....but I think you mean Html.Link because ActionLink mean you need to generate link from route table.

    <%= Html.Link("Remove from Cart", "#", new {onclick = "#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();"}) %>
    
    • param#1: linkText
    • param#2: href
    • param#3: htmlAttributes

      public static string Link(this HtmlHelper htmlHelper, string linkText, string href, object htmlAttributes) {

      TagBuilder tagBuilder = new TagBuilder("a"){ InnerHtml = linkText; }

      tagBuilder.MergeAttributes(htmlAttributes);

      tagBuilder.MergeAttributes("href", href);

      return tagBuilder.ToString(TagRenderMode.Normal);

      }

    and don't forget to <%@ Import Namespace="xxxxxx" %> on the view in order to use the extension method.