简体   繁体   English

asp .net @ Html.ActionLink动态ID(参数)

[英]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})? 在JS和ASP.NET中,如何使路由ID动态@ Html.ActionLink(“编辑”,“编辑”,新的{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)和前端(javascript)执行,它们的执行时间不同,因此无法正常工作。

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) Razor将被触发以创建html,直到它被javascript击中,一旦javascript被命中,Razor就无法与其进行交互(因为您的item.Id是由javascript生成的,因此Razor当时已经完成了该操作。 )

One way to achieve what you want is generate the base url using Razor and append the id in javascript. 实现您想要的一种方法是使用Razor生成基本URL并将id附加到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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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