I’ve been trying to figure out why this isn’t valid (according to VS2008).
//Global variable
var sortFields;
$(document).ready(function() {
sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
//Other Code here...
});
My HtmlHelper code
public static string ToJson(object obj)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(obj);
return json;
}
The Helper is generating valid Json (confirmed it), but when I’m trying to add anything else to the function, VS2008 complains about all sort kinds of stuff, can’t align the code correctly and so on when closing brackets, and as soon as I comment this out it works. However, the code works fine, even if VS2008 complains about it.
Is this just VS2008 that is crap with JQuery or am I actually doing something wrong?
The designer is generally going to get a bit upset when
<%=is in script (and it sometimes gets snarly when it is in attribute values). If the output is correct, and you’re happy with the way the html is constructed, then I wouldn’t stress.Ultimately, though; how does VS know that
ToJsonis going to return something sensible? It could return"((((((("which would really screw up the javascript. That is why it is unhappy.