I need to escape a double quote in inline c# within javascript. Code is below:
if (\"<%= TempData[\"Message\"]%>\" == \"\") {
// code
};
I have addressed this by writing a HtmlHelper that encodes the strings to a format acceptable in Javascript:
public static string JSEncode(this HtmlHelper htmlHelper, string source)
{
return (source ?? "").Replace(@"'", @"\'").Replace(@"""", @"\""").Replace(@"&", @"\&").Replace(((char)10).ToString(), "
");
}
Then, in your view:
if ('<%= Html.JSEncode( TempData["Message"] ) %>' == "") {
// code
};