是否可以使用包含元素的 ActionLink?
正如问题所说,是否可以使用包含元素的 ActionLink,如果不能,实现它的最佳方法是什么?
例如,假设我有一个 Span
元素,我希望整个元素成为一个超链接...以下工作:
<a href="/Site/Page/@Model.ID?Type=test">
<span class="box5">Click anywhere in this box</span> </a>
想象一下 span/css 类 box5 使这个大...最初是一个DIV,但是,我发现它不遵循标准,这似乎遵循正常。
这渲染并工作得很好,但是,有没有办法使用 ActionLink 来代替呢?我试图猜测语法,例如(从表单复制):
@using (Html.Actionlink){<span class="box5">Click anywhere in this box</span>}
以及许多其他组合,但没有任何运气。
现在,我的手动 HTML 解决方法工作正常,我很高兴离开它,但是,是否可以使用 MVC ActionLink,如果可以,我是否应该担心/会有什么好处吗?
As the question says, Is it possible to use an ActionLink containing an element, and if not, what is the best way to achieve it?
For example, lets say I have a Span
element, I want the whole thing to be a hyperlink... The following works:
<a href="/Site/Page/@Model.ID?Type=test">
<span class="box5">Click anywhere in this box</span> </a>
Imagine that span/css class box5 makes this large... Was originally a DIV, but, I found that didn't follow standards and this appears to follow ok.
This renders and works fine, but, is there anyway to use an ActionLink instead? I have tried to guess the syntax such as (copying from forms):
@using (Html.Actionlink){<span class="box5">Click anywhere in this box</span>}
and many other combinations without any luck.
Now, my manual HTML workaround works fine and I am happy to leave it, but, is it possible to use a MVC ActionLink, and if it is, should I even worry/would there be any benefits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需使用 @Url.Action 即可:
Just use @Url.Action instead:
您可以使用一种技巧来解决这个限制
,它并不优雅,但它可以完成工作
There's a hack you can use to work around the limitation
it's not elegant, but it does the job
Html.ActionLink
只是一个普通的旧扩展方法 到HtmlHelper
对象。您可以为
HtmlHelper 创建自己的自定义扩展方法 也是如此,而且它们做起来非常简单,并且会节省您大量的时间。
Html.ActionLink
is just a plain old Extension Method to theHtmlHelper
object.You can create your own custom extension methods for
HtmlHelper
as well, and they're pretty simple to do and will save you tons of time.你不能在actionlink中指定html标签,但我可以建议你两个解决方案:
1 用您的功能扩展操作链接。 内部其他 htmal 标记(img 或 span,如您的情况)生成 href 的 actionlink
因此,您还可以实现使用 href 中的 url.content 2 使用 。这就像您的实际解决方法,但更好,因为您使用在 global.asax 中定义的规则,而不是使用“魔术字符串”
You can not specify a html tag inside actionlink, but I can suggest you two solutions:
1 extend the actionlink with your function. So you can also implement actionlink that generate href with other htmal tag inside (img or span like in your case)
2 using the url.content in the href. It's like your actual workaround but better because you use the rules you have defined in the global.asax instead of using "magic string"
只需编写
;
并在跨度之间使用 @html>ActionLink 方法它将起作用
Just Write
<span> </span>
and in between the span use the @html>ActionLink Method It will work