为什么这个 JQuery 无效?
我一直试图找出为什么这是无效的(根据 VS2008)。
//Global variable
var sortFields;
$(document).ready(function() {
sortFields = <%= CustomHtmlHelper.ToJson(ViewData["SortInfo"])%>;
//Other Code here...
});
我的 HtmlHelper 代码
public static string ToJson(object obj)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(obj);
return json;
}
Helper 正在生成有效的 Json(已确认),但是当我尝试向函数添加其他任何内容时,VS2008 会抱怨各种问题,在关闭括号时无法正确对齐代码等等,一旦我注释掉它,它就起作用了。然而,即使 VS2008 抱怨,代码也可以正常工作。 这只是VS2008对JQuery来说是垃圾还是我实际上做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当
<%=
出现在脚本中时,设计者通常会感到有点不安(当它出现在属性值中时,有时会变得混乱)。如果输出正确,并且您对 html 的构造方式感到满意,那么我就不会强调。但最终; VS 如何知道
ToJson
将返回一些合理的内容?它可能会返回"(((((("
),这会真正搞砸 JavaScript。这就是它不高兴的原因。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
ToJson
is going to return something sensible? It could return"((((((("
which would really screw up the javascript. That is why it is unhappy.MrW,
你这里缺少一个括号:
它应该是:
另外,返回一个 jsonresult 可能会更好?另外,在 CustomHtmlHelper 周围添加单引号,如上所示。
MrW,
you,ve got a missing bracket here:
it should be:
also, it may be better to return a jsonresult?? Also, add single quotes around the CustomHtmlHelper as shown above.