简体   繁体   中英

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

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

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>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM