0

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.

1 Answer 1

5

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", "")')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution ,this is working except console.log('@Model.TestProperty'); ,this will throw same error.
Oh, duh. Not sure what I was thinking there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.