jquery-tmpl 模板中的 ASP.NET MVC ActionLink

发布于 2024-11-04 23:32:23 字数 470 浏览 2 评论 0原文

我定义了一个jquery-tmpl:

<script id="postTemplate" type="text/x-jquery-tmpl">
     <div class="div-msg-actions-inner">
          @Html.ActionLink("Edit", "Edit", "Post", new { postId = "${PostId}" }, new { @class = "button" })
          @Html.ActionLink("Reply", "Reply", "Post", new { topicId = "${TopicId}" }, new { @class = "button" })
     </div>
 </script>

操作链接导致“$”被编码为“%24”。有没有办法解决这个问题,以便正确替换我的操作链接中的 ID?

I have a jquery-tmpl defined:

<script id="postTemplate" type="text/x-jquery-tmpl">
     <div class="div-msg-actions-inner">
          @Html.ActionLink("Edit", "Edit", "Post", new { postId = "${PostId}" }, new { @class = "button" })
          @Html.ActionLink("Reply", "Reply", "Post", new { topicId = "${TopicId}" }, new { @class = "button" })
     </div>
 </script>

The action link results in the "$" being encoded into "%24". Is there a way around this so the ID in my action link will get replaced correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

离鸿 2024-11-11 23:32:23

继续发送相同的路由值参数,但将此脚本放在布局的末尾。它将替换每个页面中所有模板中的所有 ${propName} 字符串。

 $("script[type='text/x-jQuery-tmpl']").text(function (i, oldText) {
               return oldText.replace(/(%24%7B.+?%7D)&/gi, "{$1}&")
                             .replace(/%24%7B(.+?)%7D/gi, "${$1}");
});  

Continue sending route value parameter the same, but Put this script in the end of your layout. it will replace all the ${propName} strings back in all templates in every page.

 $("script[type='text/x-jQuery-tmpl']").text(function (i, oldText) {
               return oldText.replace(/(%24%7B.+?%7D)&/gi, "{$1}&")
                             .replace(/%24%7B(.+?)%7D/gi, "${$1}");
});  
探春 2024-11-11 23:32:23
@Html.ActionLink("Edit", "Edit", "Post", new { postId = "999" }, new { @class = "post-button", })
@Html.ActionLink("Reply", "Reply", "Post", new { topicId = "888" }, new { @class = "reply-button" })

...

$("#postTemplate").text($("#postTemplate").text().replace("999", "${PostId}"));
$("#postTemplate").text($("#postTemplate").text().replace("888", "${TopicId}"));

这是我最终使用的解决方案。

@Html.ActionLink("Edit", "Edit", "Post", new { postId = "999" }, new { @class = "post-button", })
@Html.ActionLink("Reply", "Reply", "Post", new { topicId = "888" }, new { @class = "reply-button" })

...

$("#postTemplate").text($("#postTemplate").text().replace("999", "${PostId}"));
$("#postTemplate").text($("#postTemplate").text().replace("888", "${TopicId}"));

This is the solution I ended up using.

羁〃客ぐ 2024-11-11 23:32:23

实现此目的的一种方法是跳过 ActionLink Helper 并使用简单的 HTML 锚标记,例如,

<a href='@Url.Content("~/Post/Edit/${PostId}")'>Post</a>
<a href='@Url.Content("~/Post/Reply/${TopicId}")'>Reply</a>

我知道这并不理想,但这对我有用。

One way you could achieve this is to skip the ActionLink Helper and use a simple HTML anchor tag e.g.

<a href='@Url.Content("~/Post/Edit/${PostId}")'>Post</a>
<a href='@Url.Content("~/Post/Reply/${TopicId}")'>Reply</a>

Not ideal I know but this worked for me.

七颜 2024-11-11 23:32:23

Html.RawUrl.Action 结合使用

您是否尝试过将 Html.RawUrl.Action 结合使用?因此,您不必使用 Html.ActionLink 创建链接,而是生成常规 HTML,并且不对特定链接/锚点的 URL 进行编码。

<a href="@Html.Raw(Url.Action("Edit", "Post", new { postId = "${PostId}"}, new { @class = "button" }))">Edit</a>

这应该将模板变量保留在模板内。

Html.RawHtml.ActionLink 结合使用

我想您已经尝试过:

@Html.Raw(Html.ActionLink("Edit", "Edit", "Post", new { postId = "${PostId}" }, new { @class = "button" }))

Using Html.Raw with Url.Action

Have you tried using Html.Raw in combination with Url.Action? So instead of creating links with Html.ActionLink you rather generate regular HTML and don't encode URL of that particular link/anchor.

<a href="@Html.Raw(Url.Action("Edit", "Post", new { postId = "${PostId}"}, new { @class = "button" }))">Edit</a>

This should keep the template variable inside your template.

Using Html.Raw with Html.ActionLink

I suppose you've tried this:

@Html.Raw(Html.ActionLink("Edit", "Edit", "Post", new { postId = "${PostId}" }, new { @class = "button" }))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文