Escaping double quotes for JS within RazorHow to display encoded HTML as decoded in MVC 3 Razor?When is a CDATA section necessary within a script tag?When are you supposed to use escape instead of encodeURI / encodeURIComponent?How to escape braces (curly brackets) in a format string in .NETWhen to use double or single quotes in JavaScript?Can I escape a double quote in a verbatim string literal?How do I import a namespace in Razor View Page?Escape @ character in razor view engineEscape curly brace '{' in String.FormatUsing Razor within JavaScriptRegex for string with quotes
How to remove the first colon ':' from a timestamp?
How to make aluminium monostearate?
What details should I consider before agreeing for part of my salary to be 'retained' by employer?
ArcPy Delete Function not working inside for loop?
Is it legal for a supermarket to refuse to sell an adult beer if an adult with them doesn’t have their ID?
Sending a photo of my bank account card to the future employer
What happens if a company buys back all of its shares?
How possible is a successful landing just with 1 wing?
Why don't commercial aircraft adopt a slightly more seaplane-like design to allow safer ditching in case of emergency?
How fast does a character need to move to be effectively invisible?
Pi 3 B+ no audio device found
How can the electric potential be zero at a point where the electric field isn't, if that field can give a test charge kinetic energy?
What is the word for "event executor"?
What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?
How to have a continuous player experience in a setting that's likely to favor TPKs?
Why must one call join() or detach() before thread destruction?
Intel 8080-based home computers
Where do the electrons come from to make the carbon stable during bombardment of alpha particles on beryllium
How to honestly answer questions from a girlfriend like "How did you find this place" without giving the impression I'm always talking about my exes?
Closure in a topological space
pg_ctl hangs over ssh
Kepler space telescope undetected planets
A verb to describe specific positioning of three layers
How to find abandoned railways in Google Maps?
Escaping double quotes for JS within Razor
How to display encoded HTML as decoded in MVC 3 Razor?When is a CDATA section necessary within a script tag?When are you supposed to use escape instead of encodeURI / encodeURIComponent?How to escape braces (curly brackets) in a format string in .NETWhen to use double or single quotes in JavaScript?Can I escape a double quote in a verbatim string literal?How do I import a namespace in Razor View Page?Escape @ character in razor view engineEscape curly brace '{' in String.FormatUsing Razor within JavaScriptRegex for string with quotes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am using GoJS library for drawing a graph. I am trying to display the following JavaScript code on the page (that's what I need to see as the page source when it's finished loading):
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
This code is generated on the server using razor syntax like this:
@
string nodes = "";
string links = "";
string childName;
foreach (Node node in Model.FullGraph)
nodes += " key: "" + node.NodeName + "",";
@foreach (uint childId in node.ChildrenIds)
childName = Model.FullGraph.Find(x => x.NodeId == childId).NodeName;
links += " from: "" + node.NodeName + "", to: "" + childName + "",";
myDiagram.model = new go.GraphLinksModel([@nodes], [@links]);
However, after loading the resulting page looks like this in HTML:
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
Escaping with backslash doesn't work, the code still replaces double quotes with " and therefore the graph is not drawn. When I manually replace them back to normal double quotes using developer tools in the browser, the graph is drawn.
I've also tried using @ for escaping, and Html.Raw() as well. Nothing works. Does anyone have some suggestions? Thanks for help in advance!
javascript c# asp.net-core razor
add a comment |
I am using GoJS library for drawing a graph. I am trying to display the following JavaScript code on the page (that's what I need to see as the page source when it's finished loading):
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
This code is generated on the server using razor syntax like this:
@
string nodes = "";
string links = "";
string childName;
foreach (Node node in Model.FullGraph)
nodes += " key: "" + node.NodeName + "",";
@foreach (uint childId in node.ChildrenIds)
childName = Model.FullGraph.Find(x => x.NodeId == childId).NodeName;
links += " from: "" + node.NodeName + "", to: "" + childName + "",";
myDiagram.model = new go.GraphLinksModel([@nodes], [@links]);
However, after loading the resulting page looks like this in HTML:
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
Escaping with backslash doesn't work, the code still replaces double quotes with " and therefore the graph is not drawn. When I manually replace them back to normal double quotes using developer tools in the browser, the graph is drawn.
I've also tried using @ for escaping, and Html.Raw() as well. Nothing works. Does anyone have some suggestions? Thanks for help in advance!
javascript c# asp.net-core razor
1
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
You don't have to create object literals by manually placing quotes. Usevar array = @Html.Raw(Json.Serialize(Model.YourList))to conver the list to an array. Then create those 2 arrays in javascript
– adiga
Mar 26 at 9:01
Also, where iscolorin your loop?
– adiga
Mar 26 at 9:03
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23
add a comment |
I am using GoJS library for drawing a graph. I am trying to display the following JavaScript code on the page (that's what I need to see as the page source when it's finished loading):
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
This code is generated on the server using razor syntax like this:
@
string nodes = "";
string links = "";
string childName;
foreach (Node node in Model.FullGraph)
nodes += " key: "" + node.NodeName + "",";
@foreach (uint childId in node.ChildrenIds)
childName = Model.FullGraph.Find(x => x.NodeId == childId).NodeName;
links += " from: "" + node.NodeName + "", to: "" + childName + "",";
myDiagram.model = new go.GraphLinksModel([@nodes], [@links]);
However, after loading the resulting page looks like this in HTML:
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
Escaping with backslash doesn't work, the code still replaces double quotes with " and therefore the graph is not drawn. When I manually replace them back to normal double quotes using developer tools in the browser, the graph is drawn.
I've also tried using @ for escaping, and Html.Raw() as well. Nothing works. Does anyone have some suggestions? Thanks for help in advance!
javascript c# asp.net-core razor
I am using GoJS library for drawing a graph. I am trying to display the following JavaScript code on the page (that's what I need to see as the page source when it's finished loading):
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
This code is generated on the server using razor syntax like this:
@
string nodes = "";
string links = "";
string childName;
foreach (Node node in Model.FullGraph)
nodes += " key: "" + node.NodeName + "",";
@foreach (uint childId in node.ChildrenIds)
childName = Model.FullGraph.Find(x => x.NodeId == childId).NodeName;
links += " from: "" + node.NodeName + "", to: "" + childName + "",";
myDiagram.model = new go.GraphLinksModel([@nodes], [@links]);
However, after loading the resulting page looks like this in HTML:
myDiagram.model = new go.GraphLinksModel([
key: "Node26", color: "lightgreen",
key: "Node25", color: "lightgreen",
key: "Node33", color: "lightgreen",
key: "Node42", color: "lightgreen"],
[
from: "Node26, to: "Node25",
from: "Node26, to: "Node33",
from: "Node33, to: "Node42",
from: "Node33, to: "Node41",
from: "Node33, to: "Node57",
from: "Node33, to: "Node25",
from: "Node33, to: "Node34"
]);
Escaping with backslash doesn't work, the code still replaces double quotes with " and therefore the graph is not drawn. When I manually replace them back to normal double quotes using developer tools in the browser, the graph is drawn.
I've also tried using @ for escaping, and Html.Raw() as well. Nothing works. Does anyone have some suggestions? Thanks for help in advance!
javascript c# asp.net-core razor
javascript c# asp.net-core razor
asked Mar 26 at 8:56
ShHolmesShHolmes
1458 bronze badges
1458 bronze badges
1
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
You don't have to create object literals by manually placing quotes. Usevar array = @Html.Raw(Json.Serialize(Model.YourList))to conver the list to an array. Then create those 2 arrays in javascript
– adiga
Mar 26 at 9:01
Also, where iscolorin your loop?
– adiga
Mar 26 at 9:03
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23
add a comment |
1
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
You don't have to create object literals by manually placing quotes. Usevar array = @Html.Raw(Json.Serialize(Model.YourList))to conver the list to an array. Then create those 2 arrays in javascript
– adiga
Mar 26 at 9:01
Also, where iscolorin your loop?
– adiga
Mar 26 at 9:03
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23
1
1
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
You don't have to create object literals by manually placing quotes. Use
var array = @Html.Raw(Json.Serialize(Model.YourList)) to conver the list to an array. Then create those 2 arrays in javascript– adiga
Mar 26 at 9:01
You don't have to create object literals by manually placing quotes. Use
var array = @Html.Raw(Json.Serialize(Model.YourList)) to conver the list to an array. Then create those 2 arrays in javascript– adiga
Mar 26 at 9:01
Also, where is
color in your loop?– adiga
Mar 26 at 9:03
Also, where is
color in your loop?– adiga
Mar 26 at 9:03
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23
add a comment |
1 Answer
1
active
oldest
votes
From my curiosity I have tried different options as well in the meantime, let me share the iterations what I have tested.
I have built a small class for testing:
public class ColoredObject
public string key get; set;
public string color get; set;
For testing purposes created a list where adding 2 test objects:
var list = new List<ColoredObject>()
new ColoredObject()
key = "Node26",
color = "lightgreen"
,
new ColoredObject()
key = "Node25",
color = "lightgreen"
;
Apostrophe version:
Then creating the JSON object in Razor:
string nodesWithApostrophe = "[";
foreach (var i in list)
nodesWithApostrophe += " key: '" + i.key + "', color: '" + i.color + "',";
nodesWithApostrophe += "]";
So the first solution which is working fine and logging to the console properly the created array as expected is the following:
let jsonObject = @Html.Raw(nodesWithApostrophe);
console.log('testJson with apostrophe', jsonObject);

Double quotes version:
I was interested how to create object with double quotes also, so I have changed to escape chars in foreach:
string nodesWithDQ = "[";
foreach (var i in list)
nodesWithDQ += " key: "" + i.key + "", color: "" + i.color + "",";
nodesWithDQ += "]";
Also doing the same in JavaScript made it work:
let jsonObject = @Html.Raw(nodesWithDQ);
console.log('testJson with double quotes', jsonObject);

add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353116%2fescaping-double-quotes-for-js-within-razor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From my curiosity I have tried different options as well in the meantime, let me share the iterations what I have tested.
I have built a small class for testing:
public class ColoredObject
public string key get; set;
public string color get; set;
For testing purposes created a list where adding 2 test objects:
var list = new List<ColoredObject>()
new ColoredObject()
key = "Node26",
color = "lightgreen"
,
new ColoredObject()
key = "Node25",
color = "lightgreen"
;
Apostrophe version:
Then creating the JSON object in Razor:
string nodesWithApostrophe = "[";
foreach (var i in list)
nodesWithApostrophe += " key: '" + i.key + "', color: '" + i.color + "',";
nodesWithApostrophe += "]";
So the first solution which is working fine and logging to the console properly the created array as expected is the following:
let jsonObject = @Html.Raw(nodesWithApostrophe);
console.log('testJson with apostrophe', jsonObject);

Double quotes version:
I was interested how to create object with double quotes also, so I have changed to escape chars in foreach:
string nodesWithDQ = "[";
foreach (var i in list)
nodesWithDQ += " key: "" + i.key + "", color: "" + i.color + "",";
nodesWithDQ += "]";
Also doing the same in JavaScript made it work:
let jsonObject = @Html.Raw(nodesWithDQ);
console.log('testJson with double quotes', jsonObject);

add a comment |
From my curiosity I have tried different options as well in the meantime, let me share the iterations what I have tested.
I have built a small class for testing:
public class ColoredObject
public string key get; set;
public string color get; set;
For testing purposes created a list where adding 2 test objects:
var list = new List<ColoredObject>()
new ColoredObject()
key = "Node26",
color = "lightgreen"
,
new ColoredObject()
key = "Node25",
color = "lightgreen"
;
Apostrophe version:
Then creating the JSON object in Razor:
string nodesWithApostrophe = "[";
foreach (var i in list)
nodesWithApostrophe += " key: '" + i.key + "', color: '" + i.color + "',";
nodesWithApostrophe += "]";
So the first solution which is working fine and logging to the console properly the created array as expected is the following:
let jsonObject = @Html.Raw(nodesWithApostrophe);
console.log('testJson with apostrophe', jsonObject);

Double quotes version:
I was interested how to create object with double quotes also, so I have changed to escape chars in foreach:
string nodesWithDQ = "[";
foreach (var i in list)
nodesWithDQ += " key: "" + i.key + "", color: "" + i.color + "",";
nodesWithDQ += "]";
Also doing the same in JavaScript made it work:
let jsonObject = @Html.Raw(nodesWithDQ);
console.log('testJson with double quotes', jsonObject);

add a comment |
From my curiosity I have tried different options as well in the meantime, let me share the iterations what I have tested.
I have built a small class for testing:
public class ColoredObject
public string key get; set;
public string color get; set;
For testing purposes created a list where adding 2 test objects:
var list = new List<ColoredObject>()
new ColoredObject()
key = "Node26",
color = "lightgreen"
,
new ColoredObject()
key = "Node25",
color = "lightgreen"
;
Apostrophe version:
Then creating the JSON object in Razor:
string nodesWithApostrophe = "[";
foreach (var i in list)
nodesWithApostrophe += " key: '" + i.key + "', color: '" + i.color + "',";
nodesWithApostrophe += "]";
So the first solution which is working fine and logging to the console properly the created array as expected is the following:
let jsonObject = @Html.Raw(nodesWithApostrophe);
console.log('testJson with apostrophe', jsonObject);

Double quotes version:
I was interested how to create object with double quotes also, so I have changed to escape chars in foreach:
string nodesWithDQ = "[";
foreach (var i in list)
nodesWithDQ += " key: "" + i.key + "", color: "" + i.color + "",";
nodesWithDQ += "]";
Also doing the same in JavaScript made it work:
let jsonObject = @Html.Raw(nodesWithDQ);
console.log('testJson with double quotes', jsonObject);

From my curiosity I have tried different options as well in the meantime, let me share the iterations what I have tested.
I have built a small class for testing:
public class ColoredObject
public string key get; set;
public string color get; set;
For testing purposes created a list where adding 2 test objects:
var list = new List<ColoredObject>()
new ColoredObject()
key = "Node26",
color = "lightgreen"
,
new ColoredObject()
key = "Node25",
color = "lightgreen"
;
Apostrophe version:
Then creating the JSON object in Razor:
string nodesWithApostrophe = "[";
foreach (var i in list)
nodesWithApostrophe += " key: '" + i.key + "', color: '" + i.color + "',";
nodesWithApostrophe += "]";
So the first solution which is working fine and logging to the console properly the created array as expected is the following:
let jsonObject = @Html.Raw(nodesWithApostrophe);
console.log('testJson with apostrophe', jsonObject);

Double quotes version:
I was interested how to create object with double quotes also, so I have changed to escape chars in foreach:
string nodesWithDQ = "[";
foreach (var i in list)
nodesWithDQ += " key: "" + i.key + "", color: "" + i.color + "",";
nodesWithDQ += "]";
Also doing the same in JavaScript made it work:
let jsonObject = @Html.Raw(nodesWithDQ);
console.log('testJson with double quotes', jsonObject);

answered Mar 26 at 10:03
norbitrialnorbitrial
4782 silver badges7 bronze badges
4782 silver badges7 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353116%2fescaping-double-quotes-for-js-within-razor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's not really a fix, more a work around. Replace all quotation marks with apostrophes in the javascript... they both worth the same and so it should be fine.
– Monofuse
Mar 26 at 8:59
You don't have to create object literals by manually placing quotes. Use
var array = @Html.Raw(Json.Serialize(Model.YourList))to conver the list to an array. Then create those 2 arrays in javascript– adiga
Mar 26 at 9:01
Also, where is
colorin your loop?– adiga
Mar 26 at 9:03
Thank you a lot! Apostrophe hack worked perfectly!
– ShHolmes
Mar 26 at 9:23