Home > front end >  how to encode string with spaces into html valid id?
how to encode string with spaces into html valid id?

Time:09-21

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?

CodePudding user response:

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)
  • Related