简体   繁体   English

转义+(加号)登录Kendo网格ASP.NET MVC ClientTemplate

[英]Escaping +(plus) sign in Kendo grid ASP.NET MVC ClientTemplate

I've having a problem defining a ASP.NET MVC ClientTemplate for a column in a Kendo grid because the '+' sign is being stripped out, which causes the client-side generated template to fail because of bad syntax. 我在为Kendo网格中的列定义ASP.NET MVC ClientTemplate时遇到问题,因为“+”符号被剥离,导致客户端生成的模板由于语法错误而失败。

My ClientTemplate is: 我的ClientTemplate是:

c.Template(@<text></text>).ClientTemplate("#='Hello' + Name#")

In the client, however, this gets reduced to: 但是,在客户端,这会减少到:

template: "#='Hello'   Name#"

ie the plus sign has been rendered as a space (similar to URL encoding I guess). 即加号已被渲染为空格(类似于我猜的URL编码)。 The template works perfectly if I use from JS directly (see this JSBin ). 如果我直接从JS使用该模板,则该模板非常有效(请参阅此JSBin )。

Does anyone know how I can escape the + sign so I can use it in the ClientTemplate? 有谁知道如何逃脱+符号所以我可以在ClientTemplate中使用它?

I have tried escaping using ' \\\\+ ' (gets rendered as ' \\ '), ' \\+ ' (invalid C#), &#43; 我尝试使用' \\\\+ '(呈现为' \\ '),' \\+ '(无效的C#) &#43;转义&#43; (same as + , although others such as &#44; ' work fine). (与+相同,但其他如&#44; '工作正常)。

Any ideas? 有任何想法吗? Thanks. 谢谢。

For reference I'm using Razor markup and Q2 2012 Kendo release (can't use Q3 yet as limited by jQuery version as we're upgrading from Telerik) 作为参考,我正在使用Razor标记和Q2 2012 Kendo版本(当我们从Telerik升级时,不能使用Q3但受限于jQuery版本)

NOTE: before anyone asks why I'm doing this, my ClientTemplate is actually more complex but this is a simplified example to illustrate the problem :-) 注意:在有人问为什么我这样做之前,我的ClientTemplate实际上更复杂,但这是一个简化的例子来说明问题:-)

I checked the source code 1 , the MVC wrapper is calling HttpUtility.UrlDecode on all templates before it renders them to JS. 我检查了源代码1 ,MVC包装器在将它们呈现给JS之前在所有模板上调用HttpUtility.UrlDecode So to escape your + sign (or any other reserved URL character), use percent encoding . 因此,要转义+号(或任何其他保留的URL字符),请使用百分比编码 In this case %2B . 在这种情况下%2B

c.Template(@<text></text>).ClientTemplate("#='Hello' %2B Name#")

1 v2013.3, src\\Kendo.Mvc\\Kendo.Mvc\\UI\\Grid\\Columns\\GridColumnBase.cs, line 135. I suspect this is a bug and they meant to call HttpUtility.UrlEncode . 1 v2013.3,src \\ Kendo.Mvc \\ Kendo.Mvc \\ UI \\ Grid \\ Columns \\ GridColumnBase.cs,第135行。我怀疑这是一个错误,他们打算调用HttpUtility.UrlEncode

Found a solution (possibly more of a workaround). 找到了解决方案(可能更多的解决方法)。 Rather than writing inline JavaScript I moved my code to a separate JS function and called that from the template code, eg: 我没有编写内联JavaScript,而是将代码移动到单独的JS函数中,并从模​​板代码中调用它,例如:

#=myFunctionToDoComplicatedStuff(Name)#

To be honest, for reasons of testability, reuse and general good practice this is probably the approach that should be taken anyway. 说实话,出于可测试性,重用和一般良好实践的原因,这可能是应该采取的方法。 It's a bit annoying for something trivial though! 尽管有些微不足道的事情,但有点烦人!

如果您只需要一个简单的解决方案来在ClientTemplate中进行连接以避免加号的编码问题,您可以使用这样的javascript concat函数:

c.Template(@<text></text>).ClientTemplate("#='Hello'.concat(Name)#")

您是否尝试过算术并将其置于引号之外,如下所示:

c.Template(@<text></text>).ClientTemplate("#='Hello'" + "Name#")

For what it's worth, I just ran into this and solved it by subtracting a negative value. 对于它的价值,我只是遇到了这个并通过减去负值来解决它。 Such as: 如:

kendo.parseFloat(Amount)  - -kendo.parseFloat(Tip)

I have found a solution by using an alternative plus sign . 我通过使用替代加号找到了解决方案。

c.Template(@<text></text>).ClientTemplate("#='Hello' (<span style=\\\\'font-size: 0.6em\\\\'>&#10133;</span>{ Name#")

This will result in... 这将导致......

template: "#='Hello + Name#"

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

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