Click here to Skip to main content
12,509,728 members (44,106 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Javascript Razor MVC4
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
v7
Rate this: bad
 
good
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:
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
  Permalink  
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
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...
—SA
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.
—SA
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.
—SA
Rate this: bad
 
good
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 :-)
  Permalink  
v2

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 | Mobile
Web02 | 2.8.160929.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2016
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