在 ASP.NET MVC Actionlink 中编码数据视图值
我正在使用 Microsoft Ajax Template DataView 将值绑定到模板。 我可以做到这一点,它的工作原理正如您所期望的:
<h3>{{ID}}</h3>
<p>{{Address}}</p>
但是我正在尝试构建一个包含 ID 的操作链接。
<h2><%= Html.ActionLink(Html.AttributeEncode("{{Name}}"), "Index", "Restaurant", new { Id = Html.AttributeEncode("{{ID}}") }, null)%></h2>
名称显示为我想要的链接文本,但链接不包含 ID,而是包含 %7B%7BID%7D%7D
如何正确解析 Id 并将其添加到链接中?
I am using the Microsoft Ajax Template DataView to bind values to a template. I can do this and it works as you'd expect:
<h3>{{ID}}</h3>
<p>{{Address}}</p>
However I am trying to build an action link that has the ID in it.
<h2><%= Html.ActionLink(Html.AttributeEncode("{{Name}}"), "Index", "Restaurant", new { Id = Html.AttributeEncode("{{ID}}") }, null)%></h2>
The name is shown as the link text as I wanted but the link doesn't include the ID, instead it has %7B%7BID%7D%7D
How would I get the Id to be properly parsed and added to the link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
额外的括号可能会导致它失效。 尝试将值分配给变量并使用 ActionLink 中的变量。 您可以在视图顶部进行分配,然后在整个视图中重复使用它们。 以避免到处重新编码。
It's possible that the extra brackets are throwing it off. Try assigning the values to variables and using the variables in the ActionLink. You could do the assignment at the top of the view and then reuse them throughout the view as well. to keep from having to re-encode them everywhere.
终于让它工作了,我不知道我是否愚蠢,或者是否缺少文档,但这里是如何将 dataview 值绑定到链接。
实际的 url 路由部分需要用单引号括起来,并且需要使用 sys:href 而不是 href。
Finally got it to work, I don't know if I was being stupid or if it's the lack of documentation but here is how to bind the dataview value to a link.
The actual url route part needs to be in single quotes and you need to use sys:href instead of href.
构建手动 URL 时最好包含
'<%= Url.Action("Index", "Restaurant")%>'
以避免应用程序名称和 URL 冲突的问题。Its good practice to include
'<%= Url.Action("Index", "Restaurant")%>'
when builing a manual url to avoid problems with the application name and conflicting urls.