简体   繁体   English

在Url.Action路由值中保留$ {name}以便与jquery.tmpl一起使用?

[英]Preserve ${name} in Url.Action route values for use with jquery.tmpl?

I'm venturing a bit off the beaten path here, but it doesn't seem too unreasonable. 在这里,我正在冒险尝试一些人迹罕至的地方,但这似乎并不太合理。

In a view, with this code: 在视图中,使用以下代码:

<div>Hello ${Name}</div>

Ran through jquery.tmpl supplied with { Id: 1 , Name = " Bob " }, will render the following on the client: 通过{{ID: 1 ,Name =“ Bob ”}提供的jquery.tmpl运行,将在客户端上呈现以下内容:

<div>Hello Bob</div>

This is great and all, but let's introduce a Url: 一切都很好,但让我们介绍一个网址:

In a view, with this code: 在视图中,使用以下代码:

<div>Hello <a href="<%: Url.Action("Profile", new { id= "${Id}" }) %>">${Name}</a></div>

Renders to: 呈现给:

<div>Hello <a href="/User/Profile?id=%24%7BId%7D">${Name}</a></div>

And after ran through jquery.tmpl the encoded Id tag doesn't get picked up: 在运行jquery.tmpl之后,编码的ID标签不会被拾取:

 <div>Hello <a href="/User/Profile?id=%24%7BId%7D">Bob</a></div>

I understand why this is happening, but what I'm struggling with is a way to prevent Route Values from being encoded. 我知道为什么会这样,但是我正在努力的是一种防止对路由值进行编码的方法。 I lose the trail after digging around in the de-compiled code for some time, the actual mechanism for encoding eludes me. 在深入研究反编译代码一段时间后,我迷失了方向,真正的编码机制使我难以理解。 If it were me, I'd convert the Route Values to an HttpNameCollection and ToString it, such as using HttpUtility.Parse() . 如果是我,我会将路由值转换为HttpNameCollection并对其进行ToString ,例如使用HttpUtility.Parse()

MVC has been amazingly flexible with things like this so I find it hard to believe I can't change the encoder or wrap route value values with an MvcHtmlString or something. MVC在这种事情上具有惊人的灵活性,因此我很难相信我不能更改编码器或使用MvcHtmlString或其他东西包装路由值。

How can I get a Url.Action, either be from a UrlHelper extensions or replaced and extended encoder, preserve route values with the jquery.tmpl formats: ${value} 如何从UrlHelper扩展或替换并扩展的编码器获取Url.Action,并使用jquery.tmpl格式保留路由值: ${value}

Not the prettiest way, but you could wrap it within a Server.UrlDecode function. 不是最漂亮的方法,但是您可以将其包装在Server.UrlDecode函数中。 So your code would look like 所以你的代码看起来像

<a href="<%: Server.UrlDecode(Url.Action("Profile", new { id= "${Id}" })) %>">${Name}</a>

This would output /User/Profile?id=${id} 这将输出/User/Profile?id=${id}

An alternate way is to create your own Html Helper which will return the RAW input rather than encoding it. 另一种方法是创建自己的HTML Helper,它将返回RAW输入而不是对其进行编码。 I had to do something similar with the ActionLink . 我必须对ActionLink做类似的事情。 Take a look at my blog post about it. 看看我有关它的博客文章。 MVC ActionLink with HTML Content. 具有HTML内容的MVC ActionLink。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM