Nic
Nic

Reputation: 1089

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.

Upvotes: 0

Views: 3020

Answers (1)

Jason P
Jason P

Reputation: 27022

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", "")')

Upvotes: 5

Related Questions