0

I am trying to create a similar calendar as shown in the image below:

calendar sample image

I have the following code:

<table>

@for(var item = Model.StartDate; item <=Model.StartDate.AddDays(7); item = item.AddDays(1))
{   
    <th>@item.ToString("ddd")</th>  

    <tr>
    <td>@item.ToString("MMM") @item.ToString("dd")</td>
    </tr>
}



</table>

Which of course does not render the correct result. What can I do to produce the result shown in the image? I don't care about the in-active dates.

4
  • Your HTML is invalid. <th> need to be nested in <tr>. Also, are you just trying to create the header row? Commented Mar 1, 2016 at 19:08
  • Please check out the layout in the image which I want to create. Commented Mar 1, 2016 at 19:11
  • Yes I see the layout image, but in your code above it looks like you are only rendering out the header. Also, do you have any css? Commented Mar 1, 2016 at 19:16
  • Yes, I know I am doing it wrong. I am stuck at rendering the complete thing! Commented Mar 1, 2016 at 19:27

2 Answers 2

1

Your HTML should look more like this:

<table>
    <thead>
        <tr>
          @for(var item = Model.StartDate; item <=Model.StartDate.AddDays(7); item = item.AddDays(1))
          {   
                <th>
                    <b>@item.ToString("ddd")</b>  
                    <div>@item.ToString("MMM") @item.ToString("dd")</div>
                </th>
           }
        </tr>
    </thead>
    <tbody>
       <!-- Load rows -->
    </tbody>
</table>

That just gives you an un-styled table header. If all that time range data is just static (doesn't depend on what the StartDateis) as it appears in the image - you said the inactive doesn't matter - then just write it all out

Sign up to request clarification or add additional context in comments.

Comments

0

This should get you closer (you have to fiddle with the html and times yourself);

The View:

<table>
    <thead>
        <tr>
            @for (int i = 0; i < Model.NumberOfDays; i++)
            {
                <th>
                    @Model.StartDate.AddDays(i).ToString("ddd")
                </th>
            }
        </tr>
    </thead>
    <tbody>
        <tr>
            @for (int i = 0; i < Model.NumberOfDays; i++)
            {            
                <td>@Model.StartDate.AddDays(i).ToString("MMM") @Model.StartDate.AddDays(i).ToString("dd")</td>
            }
        </tr>
    </tbody>
</table>

The Action in the Controller:

public ActionResult Index()    
{    
    CalendarViewModel model = new CalendarViewModel
        {
            NumberOfDays = 7,
            StartDate = DateTime.Now
        };
        return View(model);

}

The Viewmodel:

public class CalendarViewModel
{
    public int NumberOfDays { get; set; }
    public DateTime StartDate { get; set; }
}

2 Comments

Your HTML is invalid. This would create 7 <thead> elements. <thead> elements also need <tr> and <th> elements. See here for reference
@zgood Thank you for your comment, I have updated the answer. Hopefully it's more correct now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.