Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. MVC Razor, back to Date and Times, List of Items, separated by Today, Tomorrow, etc. More efficient way of writing this

MVC Razor, back to Date and Times, List of Items, separated by Today, Tomorrow, etc. More efficient way of writing this

Scheduled Pinned Locked Moved ASP.NET
asp-netarchitecturehelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    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 created dateHeader 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                                                                                    
    

    }

    Richard DeemingR 1 Reply Last reply
    0
    • J jkirkerx

      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 created dateHeader 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                                                                                    
      

      }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J D 3 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Oh you can do that in Razor? That's cool!, let me try that.

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          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#.

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            D Offline
            D Offline
            deepankarbhatnagar
            wrote on last edited by
            #5

            Helpful, thanks ...

            hi

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups