C# 如何将数据作为参数绑定到 ActionLink?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:16:02  来源:igfitidea点击:

How to bind data as parameter to ActionLink?

c#asp.net-mvc

提问by Dariusz

I have problem with ActionLink. I'd like to pass to my ActionLink parameter for my MessageController, for Edit action: to generate somthing like this /MessagesController/Edit/4

我的 ActionLink 有问题。我想传递给我的 MessageController 的 ActionLink 参数,用于编辑操作:生成这样的东西 /MessagesController/Edit/4

So I have ListView control with binding expression:

所以我有带有绑定表达式的 ListView 控件:

以及如何将此 ID 作为参数传递给 ActionLink 到我的控制器编辑操作?这不起作用: , null) %>

采纳答案by Nick Berardi

Try this

尝试这个

<%= Html.ActionLink("my link", "Edit", "Message", new { id = ((Message)Container.DataItem).ID }) %>

You need to put it in the RouteData to get it to show up. Note I am assuming idis one of your route parts that is in your route definition.

您需要将它放在 RouteData 中才能显示出来。注意我假设id是您的路线定义中的路线部分之一。

回答by liammclennan

In MVC you are not supposed to databind from the view in the way that you have. The data that you want to pass to the ActionLink method needs to be added to ViewData in your controller. Then in the view you retrieve it from ViewData:

在 MVC 中,您不应该以您拥有的方式从视图中进行数据绑定。要传递给 ActionLink 方法的数据需要添加到控制器中的 ViewData。然后在视图中从 ViewData 检索它:

<%= Html.ActionLink("My Edit Link", "Edit", "Message", new { id = ViewData["id"] }) %>

回答by user51355

<%= Html.ActionLink("My Edit Link", "Edit", "Message", new { id = ((Message)Container.DataItem).ID }, null) %>