Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I need to convert data from my model that is available as an C# array to a Javascript array.

I tried JavaScriptserializer and other methods I found
but nothing worked so far. Maybe I got some kind of syntax error.

My site also uses Razor Viewengine. I don'T know if that may cause the problem.

Currently I use this

JavaScript
<script type="text/javascript">
                $(function() {
 var Orte = [@string.Join(",",Model.HaltestellenListe.Select(x=>"'"+x+"'"))];


the result of this line looks like this
var Orte = [&amp;#39;Abbingwehr, Mast 1&amp;#39;,&amp;#39;Abelitz, B210/Abzw. Gr. Meer,&amp;#39;]

Thats not what I need.

I need it to be a javascript array:

var Orte = ['Abbingwehr, Mast', 'Abelitz, B210/Abzw. Gr. Meer']
Posted
Updated 28-Feb-13 1:14am
v7

This is not really encoding. Both strings are equivalent, but in first case, HTML characters are escaped by character entities. Please see:
http://en.wikipedia.org/wiki/Character_entity[^],
http://www.w3.org/TR/REC-html40/sgml/entities.html[^].

Escaping and unescaping in JavaScript: http://www.w3schools.com/jsref/jsref_unescape.asp[^].

In C#:
http://msdn.microsoft.com/en-us/library/system.web.httputility.htmlencode.aspx[^],
http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx[^].

—SA
 
Share this answer
 
Comments
apfelsine1 28-Feb-13 6:49am    
Well at some point some code in my site or EF - not sure about where it happens, cause its not all my code - DOES call encoding. Even if the only characters converted are parenthesis.
And in the sites you postet it is also called like that. So I don't understand how this may help me.

I tried "unescape"

<script type="text/javascript">
$(function() {
var Orte = [unescape(@string.Join(",",Model.HaltestellenListe.Select(x=>"'"+x+"'")))];

but it does not solve the problem.

I get the escaped sequence with

var Orte = [unescape(&#39;Abbingwehr, Mast 1&#39;,&#39;Abelitz, B210/Abzw. Gr. Meer&#39;)]
And the processing of my array still does not work

The array is still not working properly.


I also tried

var Orte = [@HttpUtility.HtmlDecode(string.Join(",",Model.HaltestellenListe.Select(x=>"'"+x+"'")))];

But I get an compiler error then sayin Method has invalid arguments
Sergey Alexandrovich Kryukov 28-Feb-13 11:43am    
I just would not call it "encoding", to avoid confusion. Just terminology. There are "encode" and "decode", but they are not related to what is called "encoding"...

I think you can sort your problem by yourself. Your question is answered, won't you agree?

If you still have a problem, please put the code and explanations to the body of the question, formatted, leave a comment here. Explain properly. "Still not working properly" is not informative. The problem is way too simple. Can use the debugger?

—SA
apfelsine1 28-Feb-13 15:55pm    
yes you are right.
Sorry-

"still not working" was refering to no valid array
anyway the problem is solved
thanks
Sergey Alexandrovich Kryukov 28-Feb-13 15:56pm    
Good. Are you accepting my answer formally?
—SA
apfelsine1 28-Feb-13 16:20pm    
you can close the thread if thats what you mean
Found it. Seems like Razor was causing this

HTML
var Orte = [@Html.Raw(string.Join(",",Model.HaltestellenListe.Select(x=>"'"+x+"'")))];



did the trick :-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900