Pass 2-dimensional array trough view

Posted by Mikael on Stack Overflow See other posts from Stack Overflow or by Mikael
Published on 2011-01-17T01:59:27Z Indexed on 2011/01/17 4:53 UTC
Read the original article Hit count: 165

Filed under:
|

Hi,

I'm trying to print a 2-dimensional array but can't figure it out.

My controller uses this code:

        public ActionResult Test(string str)
    {

         DateTimeOffset offset = new DateTimeOffset(DateTime.Now);
        offset = offset.AddHours(-5);

        string[,] weekDays = new string[7,2];

        for (int i = 0; i < 7; i++)
        {
            weekDays[i,0] = String.Format("{0:yyyy-MM-dd:dddd}", offset); //Date
            weekDays[i,1] = String.Format("{0:dddd}", offset); //Text
            offset = offset.AddHours(24);
        }

        weekDays[0,1] = "Today";

        ViewData["weekDays"] = weekDays;

        return View();
    }

Now I wan't to print this array of weekdays as a dropdown-list and i thought this would work:

                <% foreach (var item in (string[,])ViewData["weekDays"]) 
               { %>
                <option value=" <%= item[0] %> "> <%= item[1] %>  </option>
            <% } %>

But that's not the case, this code output just the first char of the string.

So anyone got a suggestion?

Thanks!

/M

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc-2