Sam
Sam

Reputation: 4484

How to call jquery function from @Html.ActionLink param routeValues

I have a text box and a button upon clicking which should retrieve the value set in the text box and pass it to a controller. I am trying to do this by calling a JQuery function which returns me the value of text box and pass as routevalues parameter. I know this does not work, but my question is there a way to modify the below code to achieve this? Basically i want to know how can I plug getValue function inside ActionLink call.

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

    var getValue = function () {

        return $("#addtoset").val();
    };
});
</script>

@Html.TextBox("addtoset", "", htmlAttributes : new { @id = "addtoset", @class = "col-sm-10" })
@Html.ActionLink("Add To Set", "AddToSet", routeValues: new { amount = getValue()}, htmlAttributes : new { @id="linkSet", @class = "col-sm-2" }) 

Upvotes: 1

Views: 7939

Answers (1)

Brunis
Brunis

Reputation: 1063

Add an onclick handler to the htmlattributes part of the ActionLink call like this:

@Html.ActionLink("Add To Set", "AddToSet", routeValues: new { amount = getValue()}, htmlAttributes : new { @id="linkSet", @class = "col-sm-2", onclick = "getValue" })

But if this is all your code you'll need to do more than that to get it to work :)

If you want the link to prevent navigation you add preventDefault() to your callbackFunction

function callbackFunc(e) {
    e.preventDefault();
    // work here
}

Upvotes: 1

Related Questions