TagHelper for passing route values as part of a link

馋奶兔 提交于 2019-12-02 20:01:27

You can use the attribute prefix asp-route- to prefix your route variable names.

Example: <a asp-action="Edit" asp-route-id="10" asp-route-foo="bar">Edit</a>

I'd like to suggest a combination of the other two answers, but with a bit of extra clarification.

You will use an attribute prefix asp-route-{name} where {name} is the name of the route parameter you want to use. In other words, if the number 5 in your route is passed into the controller as an ID value, you could have:

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.ID">Edit</a>

or if the parameter you wanted to pass to the route was item.UserName then

<a asp-controller="User" asp-action="Edit" asp-route-username="@item.UserName">Edit</a>

And if you had both parameters then

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.Id" asp-route-username="@item.UserName">Edit</a>

you can pass custom ID using below code:

<a asp-controller="User" asp-action="Edit" asp-route-id="@item.ID">Edit</a>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!