Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:26:29+00:00 2026-05-23T23:26:29+00:00

What would be the prefered way to initialize a JS array in ASP.NET MVC

  • 0

What would be the prefered way to initialize a JS array in ASP.NET MVC 3 with Razor with a value I have in my model/view model ?

For example to initialize an 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; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:26:30+00:00Added an answer on May 23, 2026 at 11:26 pm

    I don’t quite understand the relation between JS and ASP.NET MVC 3 Razor. Javascript runs on the client side no matter which technology has been used on the server to generate the page. So on javascript an array is an array.

    There are a couple of possibilities to 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 all those represent the same array. But it is an array of strings, not dates.

    If you wanted to have an array of dates, here’s what you could do:

    var activeDates = [ 
        new Date(2011, 6, 21, 0, 0, 0, 0), 
        new Date(2011, 6, 22, 0, 0, 0, 0) 
    ];
    

    Now the only relation I can see with your question to ASP.NET MVC is that you probably have some array on your view model:

    public class MyViewModel
    {
        public DateTime[] ActiveDates { get; set; }
    }
    

    that you wanted to serialize and manipulate in a javascript array. In this case here’s the syntax:

    @model MyViewModel
    <script type="text/javascript">
        var activeDates = @Html.Raw(Json.Encode(Model.ActiveDates));
    </script>
    

    Now because of the way DateTime fields are JSON serialized in .NET you will end up with the following in the generated HTML:

    var activeDates = ["\/Date(1309471200000)\/","\/Date(1311199200000)\/"];
    

    and if you wanted to convert this array of strings into an array of actual javascript dates:

    var dates = $.map(activeDates, function(date, index) {
        date = date.replace('/Date(', '').replace(')/', '');  
        return new Date(parseInt(date));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be your preferred way to concatenate strings from a sequence such that
I have the folowing question: What is the prefered way to use the status
I am wondering what is the prefered way of escaping floats nowadays. I have
What would be the best way to split a user profile model and its
What would the preferred way of programmatically determining which the currently installed version of
Would the following SQL remove also the index - or does it have to
Is there a way to declare preference in a regular expression? For example assume
What would be the preferred way to convert opacity (0 - 1) to hex
Does anyone have any suggestions for a best practice or preferred way of rolling
I have a J2ME app running on my mobile phone(client), I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.