I have a JavaScript variable in my jQuery code that contains an identifier that I need to use in my Html.ActionLink , but it does not work:
@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI", new {id = goalcard.Id},null))
I get: 'cannot resolve the' goalcard 'character', and the reason is that goalcard is a JavaScript variable.
It looks like this:
$.post('@Url.Action("Search", "SearchNKI")', data, function (result) { $("#GoalcardSearchResult tbody").empty(); result.forEach(function(goalcard) { $("#GoalcardSearchResult tbody").append( $('<tr/>', {
I tested so far, and I almost found a solution, and it looks like this:
@(Html.ActionLink("Genomför", "AnswerForm", "AnswerNKI",null, new {id = "mylink"}))
then I made a new function:
$('#mylink').click(function (goalcard) { var id = goalcard.Id; this.href = this.href + '/' + id; });
This should work, but what happens is that I have to use this click function inside the forEach loop to be able to achieve a variable target. and if I put it inside forEach , this Click function will execute many times depending on how much it has.
Thus, the result will be /AnswerNKI/AnswerForm/1/2 if there are two uppercase letters. or maybe /AnswerNKI/AnswerForm/1/2/3/4/5 if there are five shields.
But it should be /AnswerNKI/AnswerForm/1
it basically develops.
Another problem is that all other lines have /AnswerNKI/AnswerForm/ , so only the first line basically gets the identifier.
I do not know how to find a solution to fix it.
Any help is greatly appreciated.
Thanks in advance