0

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

justSteve
  • 5,444
  • 19
  • 72
  • 137

2 Answers2

1

Buttons can't act like links without javascript.
In general - that's a bad practice (search engines can't index your page correctly etc.).

I would recommend you to use anchor tags and make them look like a buttons.

But if you truly need it - this article provides an answer.

EDIT:

Sorry. Shot my answer little bit too fast.

This isn't exactly what you are asking (HtmlHelper is not involved) but that's how I would solve this problem:

in view i would define anchor (anchors without hrefs do pass W3 validation):

<a id='removefromcart_<%=row.ID%>' title='Remove from cart' 
   class='remove-link' />

in external javascript file:

   var onclick = function(event){
        event.preventDefault();
        var link = $(event.targetSource());

        //tag ids should be injected through view asp/cx
        $('#Step2_RemoveRegistrationForm input[name=id]') 
            .val(link.attr('id').split('_')[1])
    };
    $('a[id^=removefromcart]').click(onclick);  

in css:

 a {cursor:pointer;} /*anchors without href by default haven't pointer*/

I believe it would be too messy to poke around with javascript in HtmlHelpers.

EDIT2:

Anchor text is defined inside tags. I always confuse that. And it seems that targetSource() is wrong too. Try to rewrite it: event.targetSource()=>event.target.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • I'm looking for the inverse...I want a button to look like a link. – justSteve Sep 22 '09 at 11:33
  • I've tried the code as is but without some text between & a closing i don't see how anything becomes visible/clickable. I've added: remove from cart Now a link is visible but nothing fires onclick. thx – justSteve Sep 22 '09 at 17:51
  • Sorry - you are right. To add text - add it between opening&closing tags. Nothing happens because you didn't add javascript, didn't call it when document is ready or there is an error in it (i didn't check it, just wrote how i remembered it). – Arnis Lapsa Sep 22 '09 at 23:42
0

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.

Jirapong
  • 24,074
  • 10
  • 54
  • 72
  • Any chance I could get you to expand on the 'Link' vs 'ActionLink' usage? Html.Link is tossing an error: '...does not contain a definition for 'Link' and no extension method 'Link' ... Intellesense doesn't list 'Link'. I'm working against the release version of MVC - is this part of Futures? – justSteve Sep 22 '09 at 17:40
  • It is a simple extension method. I will put on my post. – Jirapong Sep 23 '09 at 02:10