asp .net @Html.ActionLink dynamic id (parameter)

337 Views Asked by At

In JS and ASP.NET, how to make the route id dynamic @ Html.ActionLink ("Edit", "Edit", new {id: item.Id})?

below the corresponding image: enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

you are mixing backend (Razor) and fronted (javascript) execution, which have different execution time, hence not working.

Razor will be fired to create the html before javascript has ever be hit and once javascript hit, Razor has no way of interacting with it (in your case the item.Id because it is generated by javascript, which by that time Razor has already finished)

One way to achieve what you want is generate the base url using Razor and append the id in javascript.

so, change the part you highlighted in your image to:

var url = '@Url.Action("Editer", "Edit")' + '?id=' + item.Id;
src += '<td><a href="'+ url +'">Edit</a></td>'