MVC 3 将模型项传递给@Html.ActionLink 文本?
如何将模型项传递给 @Html.ActionLink 文本...
@Html.ActionLink( @item.GetLink(),"Controller", "Action" )
这不起作用,如果我将其放入“”中,它就会变成字符串。有什么想法吗?
How to pass model item to @Html.ActionLink text...
@Html.ActionLink( @item.GetLink(),"Controller", "Action" )
this isn't working, if i put it in " " it becomes string. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(第一个参数中没有@字符,并交换动作和控制器)
(without @ character in first parameter, and swap action and controller)
添加到 Evgeny Levin 的答案中,您还可以下载 MvcContrib 库并强类型您的 ActionLink,如下
@ Html.ActionLink(x=>x.Action(), item.GetLink())
我更喜欢这种方法而不是魔术字符串,然后在我的解决方案配置中我创建一个模仿 Debug 的新视图,但将
MvcBuildViews
的值设置为 true,然后在编译时,如果有任何指向控制器/操作对的无效链接,则会引发编译器错误。使我免于多次推出未传递所需操作参数的代码Adding to Evgeny Levin's answer, you could also download the MvcContrib library and strongly type your ActionLinks like so
@Html.ActionLink<Controller>(x=>x.Action(), item.GetLink())
I prefer this method over magic strings, and then in my solution configurations I create a new one that mimics Debug but has the value for
MvcBuildViews
set to true, and then when you compile, if you have any invalid Links to controller/action pairs, it throws a compiler error. Has saved me many times of pushing out code that doesn't pass required Action parameters