简体   繁体   中英

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> ";

when I am trying to alert(myString) everything is ok but,

Suppose I have Model:

public class MyModel
{
   public string TestProperty{get;set;}
}

In controller I am setting TestProperty=myString in view :

@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:

http://jsfiddle.net/MhtEL/

alert('<ol>
      <li>sometext</li>
      </ol>');

Which isn't valid javascript (string literals can't span multiple lines).

You could replace the newlines first:

alert('@Model.TestProperty.Replace("\r\n", "")')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM