0
<a href="About" id="myLink" onclick="get()"> @item.cd </a>

From this link I need to save the value "cd" when I click it and then pass this value to another page to feed it to a query. I tried different solutions but without results and now I do not know how to do it.

4b0
  • 21,981
  • 30
  • 95
  • 142
Vezzu
  • 1
  • 3

2 Answers2

0

You can pass cd as argument of the onclick function. Along with this you need to pass event object to prevent the default behavior of the anchor tag.

Inside the function use window.location to navigate to new url

function get(e, elem) {
  e.preventDefault();
  window.location = 'yourURL/' + elem

}
<a href="About" id="myLink" onclick="get(event,'cd')"> @item.cd </a>
brk
  • 48,835
  • 10
  • 56
  • 78
  • I have to write this function on the second page, right? – Vezzu Mar 11 '19 at 10:33
  • In the first page – brk Mar 11 '19 at 10:34
  • Does this just pass the string '`cd`' -- or does asp.net do some magic to let javascipt access the bound string `@item.cd` that represents? – Cat Mar 11 '19 at 10:37
  • I need to pass this string a variable on the second page because the latter must be inserted in the where condition of a query – Vezzu Mar 11 '19 at 10:42
0

There are several ways

USE JS

  • store cd's value in web storage

  • push it to link (i.e: http://url?cd=value)

  • or directly put <a href="About?cd=value" id="myLink" onclick="get()"> @item.cd </a>

then you can get it by JS on the new page.

USE C#

create a function for GET method, when you click the link, submit cd's value then get it on the new page. Since I'm not sure which kind of .net you use, I can't give a more clearly solution.

Kien Pham
  • 108
  • 9