How do I conditionally add a Table Row Break in a razor page

Bryan Valencia 186 Reputation points
2024-08-09T19:07:09.03+00:00

I am making a calendar on a razor page.

Every time we hit Sunday, I want to insert "</tr><tr>" into the html.

This seems to be impossible for Razor to understand.

If I try to fix it, I get missing "{" or "}"

I just want to force a new row conditionally. Should be child's play.

	<tr>
		@foreach (EZBooks.ViewModels.BonusDay day in Model.bonusDays.OrderBy(d => d.Date))
		{

			if (day.Date.DayOfWeek == DayOfWeek.Sunday)
			{
				<tr>
			}

			<td data-dow="@day.Date.DayOfWeek.ToString()">
				<text>@day.Date.ToShortDateString()<br />@day.BonusHours<br />@day.BonusDollars.ToString("C")</text>
			</td>

			if (day.Date.DayOfWeek == DayOfWeek.Saturday)
			{
				</tr>
			}
		}
	</tr> // "End Tag is missing starting tag"

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bryan Valencia 186 Reputation points
    2024-08-09T21:49:17.2233333+00:00

    Not sure what happened, but this gives the right result:

    
    	@foreach (EZBooks.ViewModels.BonusDay day in Model.bonusDays.OrderBy(d => d.Date))
    	{
    
    		if (day.Date.DayOfWeek == DayOfWeek.Sunday || day == Model.bonusDays.First())
    		{
    			@:<tr>
    			}
    
    			<td data-dow="@day.Date.DayOfWeek.ToString()">
    				<text>
    					@day.Date.ToShortDateString()
    					<span style="font-size:9pt;">
    						<br />@day.BonusHours h  
    						<br />@day.BonusDollars.ToString("C")
    					</span>
    				</text>
    			</td>
    
    			if (day.Date.DayOfWeek == DayOfWeek.Saturday || day == Model.bonusDays.Last())
    			{
    			@:</tr>
    		}
    	}
    
    
    1 person found this answer helpful.

  2. Bryan Valencia 186 Reputation points
    2024-08-09T21:47:06.51+00:00

    I think I got it: I was almost there.

    This avoids having the foreach wrapped in a <tr> and correctly handles the first and last cases.

    NOTE: this does NOT handle the fact that if the data doesn't start on Sunday, the first row will be wrong. I'm still working on that aspect.

    This compiles, and yields a good-enough page for me to start working on formatting.

    
    	@foreach (EZBooks.ViewModels.BonusDay day in Model.bonusDays.OrderBy(d => d.Date))
    	{
    		if (day.Date.DayOfWeek == DayOfWeek.Sunday || day == Model.bonusDays.First())
    		{
    			@:<tr>
    			}
    			<td data-dow="@day.Date.DayOfWeek.ToString()">
    				<text>
    					@day.Date.ToShortDateString()
    					<span style="font-size:9pt;">
    						<br />@day.BonusHours h  
    						<br />@day.BonusDollars.ToString("C")
    					</span>
    				</text>
    			</td>
    			if (day.Date.DayOfWeek == DayOfWeek.Saturday || day == Model.bonusDays.Last())
    			{
    			@:</tr>
    		}
    	}
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.