Moraru Viorel
Moraru Viorel

Reputation: 342

use java script variable in ActionLink mvc3 razor

I have a textbox where i must to write a value, how to pass this value in my controller if I call my controller using ActionLink, this is my code:

view:

@Html.TextBox("tisch", "", new { @class = "teschChange"})
@Html.ActionLink("Apply Command", "ApplyCommand", "Work")

and this is te script

<script type="text/javascript">
    $(function test() {
        $(".teschChange").change(function () {
            var tisch = $(this).attr('value');
        });
    });
</script>

I must to send tisch in ApplyCommandController Thanks

Upvotes: 0

Views: 1670

Answers (2)

Brian Mains
Brian Mains

Reputation: 50728

Instead of using an action link, use a standard link:

<a id="l" href="@Url.Action("ApplyCommand", "Work")">Apply Command</a>

Then, you can use this script:

<script type="text/javascript">
    $(function test() {
        $(".teschChange").change(function () {
            var tisch = $(this).attr('value');
            $("#l").attr("href", "@Url.Action("ApplyCommand", "Work")/" + tisch);
        });
    });
</script>

Which will assign a href of: /Work/ApplyCommand/tisch.

Upvotes: 1

Moraru Viorel
Moraru Viorel

Reputation: 342

this was the correct script:

 $("#l").attr("href", "@Url.Action("ApplyCommand", "Work")?tisch=" + tisch);

Upvotes: 0

Related Questions