This is a migrated thread and some comments may be shown as answers.

[Solved] Replacing/escaping/encoding special characters inside ClientTemplate

3 Answers 143 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vesselin Obreshkov
Top achievements
Rank 2
Vesselin Obreshkov asked on 15 Nov 2011, 04:12 AM
Is there a way to replace/escape/encode special characters when using ClientTemplates?

For example:

.ClientTemplate( @"<a href=""javascript: ShowOffer(<#= OfferID #>, '<#= OfferName #>');""><#= OfferName #></a>" )

The above will not work if OfferName contains a single quote (') for instance and something of the sort <#= OfferName.ToString().Replace(""'"", ""\'"") #> unfortunately doesn't work (only works for server template... obviously).

Only way I can think of is to include another field in my ViewModels, something like OfferNameEncoded but I will have to re-write a ton of stuff to do this. There has got to be a an easier way. I can't believe I hadn't thought of this and have all these broken codes all over the place. Can something like $.telerik.formatString be used to achieve this or have I been programming too much today and need to go to bed? Help!!! :/ 

Thanks!

3 Answers, 1 is accepted

Sort by
0
Eric Villemure
Top achievements
Rank 1
answered on 14 Jun 2012, 07:13 PM
We have the same problem here, is there a way to fix this?
0
Vesselin Obreshkov
Top achievements
Rank 2
answered on 14 Jun 2012, 07:14 PM
Not really, you need to escape your strings before you feed them into your JavaScript. I have added a simple property in my ViewModel to strip out special characters (JS Friendly) and using that instead...
0
Atanas Korchev
Telerik team
answered on 15 Jun 2012, 06:27 AM
Hello Vesselin,

 "OfferName" is a JavaScript variable (sort of) and you can use the String methods as you see fit. For example:

<#= OfferName.replace("foo", "bar") #>

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
General Discussions
Asked by
Vesselin Obreshkov
Top achievements
Rank 2
Answers by
Eric Villemure
Top achievements
Rank 1
Vesselin Obreshkov
Top achievements
Rank 2
Atanas Korchev
Telerik team
Share this question
or