7

How do I assign ActionLink to a jQuery button in asp.net MVC page.

<button id="Button1">Click</button>

<%: Html.ActionLink("About", "About", "Home")%>

<script language="javascript" type="text/javascript">
    $(function () {

        $("#Button1").button().click(function () {
            return false;
        });
    });
</script>
3
  • Do you mean give the button the same src as the ActionLink? Commented May 15, 2012 at 17:52
  • On click on jQuery button I want to load view 'About'. I am not sure if I need Html.ActionLink there. Commented May 15, 2012 at 18:02
  • yes you don't need that ActionLink, check my answer :) Commented May 15, 2012 at 18:07

4 Answers 4

8

Based on your comments, you just want to go to the About action in the Home controller on button click, you can do the following and remove the ActionLink:

$(function () {
    $("#Button1").click(function () {
        location.href = '<%= Url.Action("About", "Home") %>';
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting error CS1002: ; expected on location.href = '<% Url.Action("About", "Home") %>';
Try adding a ; after Url.Action
Thanks no error now, but nothing happens after I click the button. I tried to view the source, it shows $("#Button1").button().click(function () { window.location.href = ''; });
@CoolArchTek ah it turns out you don't need the semi colon, you just need the = after <%, check my new edit I tested and it works :) I'm used to RAZOR syntax hehe :)
No problems, glad I could help!
2

Give the anchor an id:

<%: Html.ActionLink("About", "About", "Home", null, new { id = "Button1"})%>

Using this overload:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes // <==============
)

3 Comments

Do I need to have anchor (Html.ActionLink(....) )? Is it possible to write something in jQuery button javascript to call the target page?
The anchor is appearing as normal link. I wanted to use jQuery UI controls (for better UI with themes etc).
@CoolArchTek. Then add the element or what ever you want an onclick event: $('#elementId').click(function(){ /* What you want here... */}) Note that you should ask one question in a thread...
0

The button method of jQuery works for a tag too, try this:

<%: Html.ActionLink("About", "About", "Home", null, new { id="Button1" })%>

<script language="javascript" type="text/javascript">
    $(function () {

        $("#Button1").button().click(function () {
            return false;
        });
    });
</script>

3 Comments

you meant jquery UI not jquery.
Dude, he is using jQuery UI, but Ok, you think it's wrong like you said!
my mistake, I edited the question. Your answer was a bit unclear to me.
0
<%: Html.ActionLink("About", "About", "Home", new { @class = "yourclass" })%>

You can try attaching a class to the actionlink and you can select it with query. If you want to use a class.

3 Comments

It's not the right overload... :(. You gave that parameters as the routeValues...
That should still work. I just tried it.. unless I'm wrong since I'm using razor :O
Adding routesValue will no compile asp.net mvc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.