Wednesday, 1 February 2017

MVC - Fix error - JavaScript critical error at line Unterminated string constant


Error:
JavaScript critical error at line 72, column 38 in http://localhost:50349/\n\nSCRIPT1015: Unterminated string constant

Watch solution on YouTube:



I have the following code in my View file
        </footer>

        @{
            var message = ViewData["Message"] ?? string.Empty;
        }
        <script type="text/javascript">
            var message = '@message';
            if (message)
                alert(message);
        </script>

and I am passing message from my controller like this:
ViewData["Message"] = "Message 1 \n Message 2";


To fix it replace
ViewData["Message"] = "Message 1 \n Message 2";

with
ViewData["Message"] = "Message 1 \\n Message 2";



No comments:

Post a Comment