I have a variable within item.Name that contains the string \"It\'s Tuesday!\". To alleviate javascript errors, in the c# controller I have already escaped this single quot
Create an extension method you can use or simply encode directly. I would not encode prior to the view getting it unless you have a separate property in your viewmodel meant for this (not a bad idea()
This is not tested but something along these lines
public static class HtmlExtensions
{
public static IHtmlString JavaScriptEncode(this HtmlHelper html, string item)
{
return new HtmlString(HttpUtility.JavaScriptStringEncode(item));
}
}
You can then just call @Html.JavaScriptEncode(yourProperty) from the view or simply reference your encoded property name.