title: ASP.NET MVC - Order By Date
body: <p>I am trying to sort table (columns: ID, Name, SermonDate, SermonTitle, BibleReading, VideoLink, BulletinLink) by date (SermonDate) so the data with the newest date comes on top and display it in my MVC application. </p>
  
  <p>After some researching, I've tried putting the below with reference to <a href="https://stackoverflow.com/questions/22095694/order-by-date-asp-net-mvc-5">this</a> and <a href="https://stackoverflow.com/questions/1433088/asp-net-mvc-model-orderby-date-has-no-effect">this</a> but nothing worked for me - probably because I misunderstood and put wrong code though.</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index()
  {
       return View(await _context.Sermon.OrderBy(sermon =&gt; 
       DateTime.Now).Take(5).ToListAsync());
  }
  </code></pre>
  
  <p>My current controller (SermonsController.cs):</p>
  
  <pre><code>public SermonsController(BKPCContext context)
      {
          _context = context;    
      }
  
      public async Task&lt;IActionResult&gt; Index()
      {
          return View(await _context.Sermon.OrderBy(sermon =&gt; DateTime.Now).Take(5).ToListAsync());
      }
  
      public async Task&lt;IActionResult&gt; Details(int? id)
      {
          if (id == null)
          {
              return NotFound();
          }
  
          var sermon = await _context.Sermon
              .SingleOrDefaultAsync(m =&gt; m.ID == id);
          if (sermon == null)
          {
              return NotFound();
          }
  
          return View(sermon);
      }
  
      public IActionResult Create()
      {
          return View();
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create([Bind("ID,Name,SermonDate,SermonTitle,BibleReading,VideoLink,BulletinLink")] Sermon sermon)
      {
          if (ModelState.IsValid)
          {
              _context.Add(sermon);
              await _context.SaveChangesAsync();
              return RedirectToAction("Index");
          }
          return View(sermon);
      }
  </code></pre>
  
  <p>And the html table in /Sermons/Index.cshtml as below:</p>
  
  <pre><code>&lt;table class="table"&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.SermonDate)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.SermonTitle)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.BibleReading)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.VideoLink)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.BulletinLink)
          &lt;/th&gt;
          &lt;th&gt;
  
          &lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Name)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.SermonDate)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.SermonTitle)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.BibleReading)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.VideoLink)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.BulletinLink)
          &lt;/td&gt;
          &lt;td id="aspButton"&gt;
              &lt;a role="button" id="btn-primary" class="btn btn-primary" asp-action="Edit" asp-route-id="@item.ID"&gt;Edit&lt;/a&gt;
              &lt;a role="button" id="btn-danger" class="btn btn-danger" asp-action="Delete" asp-route-id="@item.ID"&gt;Delete&lt;/a&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      }
  &lt;/tbody&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any help would really be appreciated.</p>