title: JavaScript variable carry an ID that I need to use in a Html.ActionLink
body: <p>I have a JavaScript variable in my jQuery code that contains an ID that I need to use in my <code>Html.ActionLink</code> but it's not working:</p>
  
  <pre><code>@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI", new {id = goalcard.Id},null))
  </code></pre>
  
  <p>I get: 'cannot resolve symbol "goalcard"', and the reason is that goalcard is a JavaScript variable.</p>
  
  <p>This is what it looks like:</p>
  
  <pre class="lang-js prettyprint-override"><code>$.post('@Url.Action("Search", "SearchNKI")', data, function (result) {
      $("#GoalcardSearchResult tbody").empty();
      result.forEach(function(goalcard) {
          $("#GoalcardSearchResult tbody").append(
              $('&lt;tr/&gt;', {
              // steg Create a row for each result 
                  html: "&lt;td&gt;" + goalcard.Name +
                  "&lt;/td&gt;&lt;td&gt;" + goalcard.Customer +
                  "&lt;/td&gt;&lt;td&gt;" + goalcard.PlannedDate +
                  "&lt;/td&gt;&lt;td&gt;" + goalcard.CompletedDate +
                  "&lt;/td&gt;&lt;td&gt;" + '@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI", new {id = goalcard.Id},null))' + "&lt;/td&gt;"
          }));
      });
  });
  </code></pre>
  
  <p>I have been testing for while now and I almost found a solution and it looks like this:</p>
  
  <pre><code>@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI",null, new {id = "mylink"}))
  </code></pre>
  
  <p>then I made a new function:</p>
  
  <pre><code>$('#mylink').click(function (goalcard) {
      var id = goalcard.Id;
      this.href = this.href + '/' + id;
  });
  </code></pre>
  
  <p>This should work, but what happens is that I have to use this click function inside the <code>forEach</code> loop to be able to reach to goalcard variable. and If I put it inside the <code>forEach</code>, this Click function will get executed many times depending on how many goalcard there are.</p>
  
  <p>So the result would be  <code>/AnswerNKI/AnswerForm/1/2</code> if there are two goalcards.
  or maybe <code>/AnswerNKI/AnswerForm/1/2/3/4/5</code> if there are five goalcards.</p>
  
  <p>But it should be <code>/AnswerNKI/AnswerForm/1</code></p>
  
  <p>it basically adds up.</p>
  
  <p>Another problem is that all other rows have  <code>/AnswerNKI/AnswerForm/</code> so only the first row basically gets an id.</p>
  
  <p>I have no idea how to find a solution to fix that.</p>
  
  <p>Any kind of help is very appreciated.</p>
  
  <p>Thanks in advance</p>