3

I want to use the JQuery variable/value in the ASP.NET MVC Action link, So I need to use $("#JobGR :radio:checked").val() in below line of code :

     @Html.ActionLink("Insert", "InsertPersonJob","Reg" , new {JobNo=$("#JobGR :radio:checked").val(), PersonID=User.Identity.Name },null)

I receive an error on $ , Please help me how I can solve this issue.

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
Bashar Abu Shamaa
  • 1,998
  • 2
  • 21
  • 36

2 Answers2

1

The problem is that ActionLink is generated on server side. JQuery exist only on client side. There is no simple way to do this. You have to build this link on client side to make it working.

There are some topics about this problem like:

Every above takes different approach on this problem

Community
  • 1
  • 1
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
1

You can't use dynamic javascript inside ActionLink, because ActionLink generation is in render html action.

But you can use something like placeholder:

<a href="#" data-url="@Html.ActionLink("Insert", "InsertPersonJob","Reg" , new { JobNo="{replaceMe}", PersonID=User.Identity.Name },null)" class="replacedLink">Link</a>

<script>

$("a.replacedLink").click(function(){
   window.location = $(this).attr("data-url").replace("{replaceMe}", $("#JobGR :radio:checked").val());
});

</script>
David Horák
  • 5,535
  • 10
  • 53
  • 77