0

This question is just about warnings (application is working fine). I am using Visual Studio 2012, html5, framework 4.5 Asp.Net Web Application. We can observe this issue in aspx web page when in C# page part (aspx.cs) declaring:

public string EmployeeJson { get; set; } 

and in page part (aspx) in javascript section we have:

<script type="text/javascript">
var  employee = <%= EmployeeJson %>;
 </script>

After pressing "Rebuild Solution" in Error List tab we can see a Warning with description: "Syntax error". Compiler point with red underline on semicolon (without semicolon the red line will be under "var"). Any suggestions?

2 Answers 2

2

Do this way

var employee=Object(<%= EmployeeJson %>);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It works fine with array, but when I set there a simple string like "-1", then it starts treating this variable like an object. Therefore all equations like 'if(employee=="-1")' just don't work. How to fix it?
As i answer your question by seeing EmployeeJson which is obviously JSON format that's why give you that solution but as in case of string you may use String(). Like var employee=String(<%= EmployeeJson %>);
Brilliant! You save my day. Now I have a clean code without warnings. Sorry I can't upvote your answer (need at least 15 reputation).
@romabr no problem up vote when you have reputation :p
1

does it work if you do

var employee = "<%= EmployeeJson %>";

1 Comment

No.It throws critical error because the value is something like this ""-1""

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.