MVC 3 将模型项传递给@Html.ActionLink 文本?

发布于 2025-01-06 16:58:48 字数 166 浏览 2 评论 0原文

如何将模型项传递给 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2025-01-13 16:58:48
@Html.ActionLink(item.GetLink(), "Action", "Controller")

(第一个参数中没有@字符,并交换动作和控制器)

@Html.ActionLink(item.GetLink(), "Action", "Controller")

(without @ character in first parameter, and swap action and controller)

开始看清了 2025-01-13 16:58:48

添加到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文