MVC Razor, back to Date and Times, List of Items, separated by Today, Tomorrow, etc. More efficient way of writing this
-
I've never done this before, and feel dumb for asking. But I'm trying to create a header that says the following below in a more efficient way that what I've got now. -- Today, March 18, 2016 -- Avatar - List Item -- Tomorrow, March 20, 2016 -- Avatar - List Item Avatar - List Item Here on my View, I created
today
as the Universal Date for Today And createddateHeader
saying that I have showed the date once, don't show it again. Problem is I have more dates to show, so that backfired on me.@{
ViewBag.Title = "Your Current Jobs";
Layout = "~/Views/Shared/Admin/_adminLayout.cshtml";
DateTime today = DateTime.Now.Date;
bool headerToday = false;
bool headerTomorrow = false;
}So now in the same view in Razor, I'm looping my List Items. In know why I don't get to tomorrow, and in the past I would of just made more vars such as
today
,tomorrow
,next day
,etc
. So my Question is: Is there better way to write this. I can't even think of the nomenclature to search for ways. I'm just trying hard to produce a better product on this project, and Razor is new to me. I really don't want to create a var for each day of the week. [edit -better razor code]<div class="jobRecords">
@foreach (var item in Model)
{@if (0 == today.CompareTo(item.Date\_Start.Date)) { if (false == headerToday) { Today - @string.Format("{0:dddd, MMMM d, yyyy}", item.Date\_Start.Date) headerToday = true; } } else if (0 == today.AddDays(1).CompareTo(item.Date\_Start.Date)) { if (false == headerTomorrow) { Tomorrow - @string.Format("{0:dddd, MMMM d, yyyy}", item.Date\_Start.Date) headerTomorrow = true; } } </div> //more Razor - build the list item, move on to next item
}
-
I've never done this before, and feel dumb for asking. But I'm trying to create a header that says the following below in a more efficient way that what I've got now. -- Today, March 18, 2016 -- Avatar - List Item -- Tomorrow, March 20, 2016 -- Avatar - List Item Avatar - List Item Here on my View, I created
today
as the Universal Date for Today And createddateHeader
saying that I have showed the date once, don't show it again. Problem is I have more dates to show, so that backfired on me.@{
ViewBag.Title = "Your Current Jobs";
Layout = "~/Views/Shared/Admin/_adminLayout.cshtml";
DateTime today = DateTime.Now.Date;
bool headerToday = false;
bool headerTomorrow = false;
}So now in the same view in Razor, I'm looping my List Items. In know why I don't get to tomorrow, and in the past I would of just made more vars such as
today
,tomorrow
,next day
,etc
. So my Question is: Is there better way to write this. I can't even think of the nomenclature to search for ways. I'm just trying hard to produce a better product on this project, and Razor is new to me. I really don't want to create a var for each day of the week. [edit -better razor code]<div class="jobRecords">
@foreach (var item in Model)
{@if (0 == today.CompareTo(item.Date\_Start.Date)) { if (false == headerToday) { Today - @string.Format("{0:dddd, MMMM d, yyyy}", item.Date\_Start.Date) headerToday = true; } } else if (0 == today.AddDays(1).CompareTo(item.Date\_Start.Date)) { if (false == headerTomorrow) { Tomorrow - @string.Format("{0:dddd, MMMM d, yyyy}", item.Date\_Start.Date) headerTomorrow = true; } } </div> //more Razor - build the list item, move on to next item
}
How about something like this:
@foreach (var day in Model.GroupBy(i => i.Date_Start.Date))
{
<div class="jobDateHeader">
if (day.Key == today)
{
<span class="label label-primary">Today - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else if (day.Key == today.AddDays(1))
{
<span class="label label-primary">Tomorrow - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else
{
<span class="label label-primary">@day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
</div>@foreach (var item in day) { // Build the list item here }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How about something like this:
@foreach (var day in Model.GroupBy(i => i.Date_Start.Date))
{
<div class="jobDateHeader">
if (day.Key == today)
{
<span class="label label-primary">Today - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else if (day.Key == today.AddDays(1))
{
<span class="label label-primary">Tomorrow - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else
{
<span class="label label-primary">@day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
</div>@foreach (var item in day) { // Build the list item here }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How about something like this:
@foreach (var day in Model.GroupBy(i => i.Date_Start.Date))
{
<div class="jobDateHeader">
if (day.Key == today)
{
<span class="label label-primary">Today - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else if (day.Key == today.AddDays(1))
{
<span class="label label-primary">Tomorrow - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else
{
<span class="label label-primary">@day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
</div>@foreach (var item in day) { // Build the list item here }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Works like a champ! I never thought of using my model again with another lambda expression. Guess I got something right on it the first time. So if I get the model right, I can continue to manipulate it through the view. Thanks Richard! I really like this MVC a lot. I haven't got to the time saving part yet because I'm trying to stay organized with reusable code modules and learning MVC at the same time. Plus being more efficient. Oh and write c# at the same time, in which now I'm use to, Struggled on a VB code fix this morning, could not remember how to write a tenary, much easier in C#.
-
How about something like this:
@foreach (var day in Model.GroupBy(i => i.Date_Start.Date))
{
<div class="jobDateHeader">
if (day.Key == today)
{
<span class="label label-primary">Today - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else if (day.Key == today.AddDays(1))
{
<span class="label label-primary">Tomorrow - @day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
else
{
<span class="label label-primary">@day.Key.ToString("dddd, MMMM d, yyyy")</span>
}
</div>@foreach (var item in day) { // Build the list item here }
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Helpful, thanks ...
hi