Nik Volk
Nik Volk

Reputation: 47

how to encode string with spaces into html valid id?

I have classitems whose unique identifier is their stringfield. I am set these elements in html using Razor(C#). I handle it for javascript. I need to convert strings from example"Mr. Smith" to id"{2342-2341-2324-...}" or at Base64"92384098230...". And then use javascript to get the value from the encoded string. How to do it in C# and Javascript?

Upvotes: 0

Views: 1502

Answers (1)

Joel Pusanur
Joel Pusanur

Reputation: 34

You could do the following-

C# - Uri.EscapeDataString(String)

And to convert the above value in JavaScript-

js - unescape(string) or decodeURI(string)

If you would like to do the reverse you can use

C# - Uri.UnEscapeDataString(string) & js - escape(string) or encodeURI (string)

Upvotes: 1

Related Questions