1

Hello all I am Using following syntax in my javascript

 var careteam = "@Html.Raw(Json.Encode(Model.CareTeamForFile))";

I am getting the following error

SyntaxError: missing ; before statement
[Break On This Error]   

var careteam = "[{"GroupId":3,"GroupName":"Physician","Users":[{"UserId":3438,"

an arrow is there indicating position.

I have used this syntax in other places where it works absolutely fine.

1 Answer 1

2

In JS you get next line:

var careteam = "[{"GroupId":3,"GroupName":"Physician","Users":[{"UserId":3438,"

If we will read this line it gives you declaration of variable var careteam = "[{", after you have GroupId":3,"GroupName":"Physician","Users":[{"UserId":3438," which does not make sense for JS.

Change the razor syntax to:

var careteam = '@Html.Raw(Json.Encode(Model.CareTeamForFile))';

So in careteam you will have a string

var careteam = '[{"GroupId":3,"GroupName":"Physician","Users":[{"UserId":3438," ... '

But my bet this is not what you want, you want to have an array in careteam, so my guess this is the right solution:

var careteam = @Html.Raw(Json.Encode(Model.CareTeamForFile));

And you will get in JS:

var careteam = [{"GroupId":3,"GroupName":"Physician","Users":[{"UserId":3438," ... 
Sign up to request clarification or add additional context in comments.

4 Comments

yea.. outcoldman your first solution works but can u please tell me why it is working in case of var fileforteam = "@Html.Raw(Json.Encode(Model.FileForMyTeam))";
String in JavaScript can be defined like var a = 'a'; or var a = "a";, no difference, just different syntax. If you need to use ' or " in it you need to escape this symbol by var a = 'a\'b'; or var a = "a\"b";, but you also can change it to var a = "a'b"; or var a = 'a"b';, and you will not need to escape these symbols, because in case where you use ' you need to escape ', but " is fine and other way. Hope this is clear :)
yes fourth one is the correct way with respect of JS and HTML but in razor i am getting syntax error at ; when i declare var careteam = @Html.Raw(Json.Encode(Model.CareTeamForFile));
For the syntax error, change var careteam = @Html.Raw(Json.Encode(Model.CareTeamForFile)); to var careteam = function () { return @Html.Raw(Json.Encode(Model.CareTeamForFile)); }

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.