ASP.NET MVC 3 Razor : Initialize a JavaScript array -


what prefered way initialize js array in asp.net mvc 3 razor value have in model/view model ?

for example initialize array of strings representing dates :

<script type="text/javascript">     var activedates = ["7-21-2011", "7-22-2011"]; </script> 

with

public class myviewmodel {       public datetime[] activedates { get; set; } } 

i don't quite understand relation between js , asp.net mvc 3 razor. javascript runs on client side no matter technology has been used on server generate page. on javascript array array.

there couple of possibilities define arrays of objects in javascript

var activedates = [ '7-21-2011', '7-22-2011' ]; 

or:

var activedates = new array(); activearrays.push('7-21-2011'); activearrays.push('7-22-2011'); 

or yet:

var activedates = new array(); activearrays[0] = '7-21-2011'; activearrays[1] = '7-22-2011'; 

at th end represent same array. array of strings, not dates.

if wanted have array of dates, here's do:

var activedates = [      new date(2011, 6, 21, 0, 0, 0, 0),      new date(2011, 6, 22, 0, 0, 0, 0)  ]; 

now relation can see question asp.net mvc have array on view model:

public class myviewmodel {     public datetime[] activedates { get; set; } } 

that wanted serialize , manipulate in javascript array. in case here's syntax:

@model myviewmodel <script type="text/javascript">     var activedates = @html.raw(json.encode(model.activedates)); </script> 

now because of way datetime fields json serialized in .net end following in generated html:

var activedates = ["\/date(1309471200000)\/","\/date(1311199200000)\/"]; 

and if wanted convert array of strings array of actual javascript dates:

var dates = $.map(activedates, function(date, index) {     date = date.replace('/date(', '').replace(')/', '');       return new date(parseint(date)); }); 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -