0

I want to pass the value of the checkbox to a action link

@Html.CheckBox("YesIwant", false)

I tried this action link :

 @Html.ActionLink("Send", "ResponseUser", "ResponseCont", new { YesIwantValue= $('#YesIwant').val })

but no chance

Any help ?

melom
  • 107
  • 1
  • 13

2 Answers2

1

The checkbox value is set (clicked) and JavaScript is run client-side, while the action link is rendered server-side. In other words, you can't do this because the two things are happening at entirely different points in time.

You would have to handle the change event of the checkbox via JavaScript, client-side, and change the link's href accordingly:

$('#YesIwant').on('change', function () {
    $('#MyLink').attr('href', newUrl);
});

Where newUrl would be the URL of the link changed to reflect the new value of YesIwant. It would be on you to compose that string, though. Razor is of no help here.

However, more likely than not, this would be better handled by using an actual form and posting the checkbox value, rather than trying to alter a link.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

You can try this :

$('#YesIwant').on('change', function () {
   location.href = '@Url.Action("ResponseUser", "ResponseCont")?YesIwantValue=' + $('#YesIwant').val();
});