Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
XML
    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
Updated 14-Feb-13 4:12am
v2

If that is your data then no problem. You missed the javascript array push method.
Please check the code
C#
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);
 
Share this answer
 
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.
carefully see the code bellow and compare with your code and try to understand your mistake
C#
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.
 
Share this answer
 
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.

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