Click here to Skip to main content
12,070,764 members (62,938 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Javascript MVC
I need to convert Viewbag data which contains a Json list in my controller to javascript array in my view. I used such a code but it gives syntax error in
    var array = @Html.Raw(Json.Encode(@ViewBag.Data));
part of the code
Here are the codes in my view:
<script>
    var array = @Html.Raw(Json.Encode(@ViewBag.Data));
       var jScriptArray = new Array();
        for (var i = 0; i < array.length; i++) {
        jScriptArray[i] = array[i];

        }
 </script>
Any help would be appreciated,thanks
Posted 14-Feb-13 4:49am
pln474273
Edited 14-Feb-13 5:12am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

carefully see the code bellow and compare with your code and try to understand your mistake
var array ={Books:[{id:1,name:'a'},{id:2,name:'b'}]};
console.log(array.Books.length);
var newArray = [];
for(var i=0, j=array.Books.length; i<j; i++){
    newArray.push(array.Books[i]);
}
console.log(newArray.length);
you use
array.lengh
there i guess any json object might be ther like Books in my code. Next you need to use push method to add element to a new array. You used
jScriptArray[i] 
Either previously you create dimension for array or use push method for allocate dynamically.
  Permalink  
Comments
pln474 15-Feb-13 2:29am
   
Thank you S.M Ahasan Habib, I think I understood the problem. My json data is like this:

[{"AutoKey":1,"MachineGroup":"Atyc","StartDate":"\/Date(1322002860000)\/","EndDate":"\/Date(1322027095000)\/","Duration":24235},{"AutoKey":2,"MachineGroup":"Fongs","StartDate":"\/Date(1322003160000)\/","EndDate":"\/Date(1322012194000)\/","Duration":9034},{"AutoKey":3,"MachineGroup":"Then","StartDate":"\/Date(1322005320000)\/","EndDate":"\/Date(1322019001000)\/","Duration":13681},{"AutoKey":4,"MachineGroup":"Numune","StartDate":"\/Date(1322008920000)\/","EndDate":"\/Date(1322031974000)\/","Duration":23053},{"AutoKey":5,"MachineGroup":"Then","StartDate":"\/Date(1322009760000)\/","EndDate":"\/Date(1322032624000)\/","Duration":22863},{"AutoKey":6,"MachineGroup":"Fongs","StartDate":"\/Date(1322010060000)\/","EndDate":"\/Date(1322067224000)\/","Duration":57163},{"AutoKey":7,"MachineGroup":"Fongs","StartDate":"\/Date(1322012220000)\/","EndDate":"\/Date(1322070357000)\/","Duration":58137},{"AutoKey":8,"MachineGroup":"Fongs","StartDate":"\/Date(1322012940000)\/","EndDate":"\/Date(1322026274000)\/","Duration":13334},{"AutoKey":9,"MachineGroup":"Then","StartDate":"\/Date(1322021640000)\/","EndDate":"\/Date(1322040622000)\/","Duration":18982},{"AutoKey":10,"MachineGroup":"Atyc","StartDate":"\/Date(1322023440000)\/","EndDate":"\/Date(1322032891000)\/","Duration":9450}]

I am beginner for this subject. So I don't know how can I give a name to json data.If you can explain this,I would be so happy.Thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If that is your data then no problem. You missed the javascript array push method.
Please check the code
var array =[{"AutoKey":1,"MachineGroup":"Atyc","StartDate":"\/Date(1322002860000)\/","EndDate":"\/Date(1322027095000)\/","Duration":24235},{"AutoKey":2,"MachineGroup":"Fongs","StartDate":"\/Date(1322003160000)\/","EndDate":"\/Date(1322012194000)\/","Duration":9034},{"AutoKey":3,"MachineGroup":"Then","StartDate":"\/Date(1322005320000)\/","EndDate":"\/Date(1322019001000)\/","Duration":13681}];
console.log(array.length);
var newArray = [];
for(var i=0, j=array.length; i<j; i++){
    newArray.push(array[i]);
}
console.log(newArray.length);
  Permalink  
Comments
pln474 15-Feb-13 4:56am
   
Thanks again but my data is came from database, I mean it is dynamic and based on user input. So I need to assign this data to a variable. I actually use mvc pattern. In my model I connect database and operate a stored procedure. My controller returns Json data according to my view which takes user input (start time and end time). Thanks again
S. M. Ahasan Habib 15-Feb-13 5:42am
   
Instead of jScriptArray[i] = array[i]; you will use jScriptArray.push(array[i]); in your code and it will work.

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.160208.1 | Last Updated 15 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