This question already has an answer here:
- Escaping a double-quote in inline c# script within javascript 3 answers
I have a C# string that needs to be passed to the page and then sent back to the server. It needs to be able to handle single quotes, double quotes and any other standard characters that might cause problems. I'm using MVC4 and razor. Right now I'm passing the string to javascript. I have something like this:
var str = '@Html.Raw(Model.SomeString.Replace("'", @"\'"))';
This works just fine but I was wondering if there's a better, more elegant way of escaping a C# string that will be passed to javascript.
I've already tried storing the value in a hidden field, like this:
@Html.Hidden(x => x.SomeString)
but then it has problems with double quotes when I go to grab the value.
EDIT: turns out the Html.Hidden issue was being caused by something else
Any help is greatly appreciated!
NOTE: must work in IE9 and 10. No ViewBags.