Click here to Skip to main content
13,092,862 members (67,666 online)
Rate this:
Please Sign up or sign in to 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

<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 27-Feb-13 11:01am
Updated 28-Feb-13 1:14am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is not really encoding. Both strings are equivalent, but in first case, HTML characters are escaped by character entities. Please see:[^],[^].

Escaping and unescaping in JavaScript:[^].

In C#:[^],[^].

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?

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

"still not working" was refering to no valid array
anyway the problem is solved
Sergey Alexandrovich Kryukov 28-Feb-13 15:56pm
Good. Are you accepting my answer formally?
apfelsine1 28-Feb-13 16:20pm
you can close the thread if thats what you mean
Sergey Alexandrovich Kryukov 28-Feb-13 16:30pm
Not really. The thread is kept, and anyone can add another answers. Look at other threads. More than one accept is usual. You accept it or not, that's all what matters. A gave you exact recipe, that's all. You if disagree, you should better give your arguments...
apfelsine1 28-Feb-13 16:37pm
sorry I am blind somehow where do I accept?
Sergey Alexandrovich Kryukov 28-Feb-13 16:39pm
Green button. You should know because you already accepted one. Thank you and good luck.
apfelsine1 28-Feb-13 16:42pm
There are two solutions. One I gave and one you gave.
But yours was not a solution for my problem so do I klick that button anyway?
Sergey Alexandrovich Kryukov 28-Feb-13 16:53pm
Yes. Don't worry. Both will be accepted.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Found it. Seems like Razor was causing this

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

did the trick :-)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100