[英]Uncaught SyntaxError: Unexpected token ILLEGAL ASP.NET mvc3
Suppose I have following string: 假设我有以下字符串:
var myString=" <ol>\\r\\n<li>Some text</li>\\r\\n</ol>
"; var myString =“ <ol>\\r\\n<li>Some text</li>\\r\\n</ol>
”;
when I am trying to alert(myString) everything is ok but, 当我尝试提醒(myString)一切正常时,
Suppose I have Model: 假设我有模型:
public class MyModel
{
public string TestProperty{get;set;}
}
In controller I am setting TestProperty=myString
in view : 在控制器中,我在视图中设置TestProperty=myString
:
@model MyModel
<script>
jQuery(document).ready(function() {
alert('@Model.TestProperty')// here I am getting error Uncaught SyntaxError: Unexpected token ILLEGAL
})
</script>
I cant figure out what is the problem ,and how to fix it . 我不知道是什么问题,以及如何解决。 Thanks a lot for your attention. 非常感谢您的关注。
Your generated javascript looks like this: 您生成的javascript如下所示:
http://jsfiddle.net/MhtEL/ http://jsfiddle.net/MhtEL/
alert('<ol>
<li>sometext</li>
</ol>');
Which isn't valid javascript (string literals can't span multiple lines). 无效的javascript(字符串文字不能跨越多行)。
You could replace the newlines first: 您可以先替换换行符:
alert('@Model.TestProperty.Replace("\r\n", "")')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.