Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8441929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:42:16+00:00 2026-06-10T08:42:16+00:00

I have following code in HomeController ; it gets latest date and shows songs

  • 0

I have following code in HomeController; it gets latest date and shows songs according to that date.

public ActionResult Index()
{           
   var topsong = db.TopSongs;
   var topDate = db.TopDates;
   var latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault();
   int dateId = topDate.OrderByDescending(d => d.DateId).FirstOrDefault().DateId;
   var topTitles = topsong.Where(s => s.DateId == latestDate.DateId).ToList();

   ViewBag.Message = "Top Songs";                                    
   ViewBag.latestDate = latestDate.Date.ToShortDateString();            

   return View(topTitles);
}

This goes into database and picks latest date — let’s say 08/25/2012 — and shows all songs corresponding to this date. This part is working fine; now I want to add a feature where the user can select previous dates or next dates from the database, and display songs according to that.

What I am trying to do is add previous and next date buttons in the View. Like this:

<h2>Songs Updated On: @ViewBag.latestDate</h2>
@Html.ActionLink("Previous Week's Songs", "Index", new { });
@Html.ActionLink("Next Week's Songs", "Index", new { });

As I am new to MVC3, can someone please tell me how can I get previous and next dates to show up? If I calculate them in the View, how can I pass them back to controller to show songs for those dates?

Here is the example dates table:
DateId Date
1 08/25/2012
2 08/20/2012
3 09/21/2012

Updated Code:

Here is code for HomeController:

 public ActionResult Index(DateTime? date = null)
        {            
            var topsong = db.TopSongs;
            var topDate = db.TopDates;

            var latestDate = (date.HasValue) ? date.Value : topDate.OrderByDescending(d => d.Date).FirstOrDefault();           


            //var latestDate = topDate.OrderByDescending(d => d.Date).FirstOrDefault();

            int dateId = topDate.OrderByDescending(d => d.DateId).FirstOrDefault().DateId;           
            var topTitles = topsong.Where(s => s.DateId == latestDate.DateId).ToList();

            /////
            ViewBag.NextDate = topDate.Where(d => d.Date > latestDate).OrderBy(d => d.Date).FirstOrDefault();
            ViewBag.PreviousDate = topDate.Where(d => d.Date < latestDate).OrderByDescending(d => d.Date).FirstOrDefault();
            ////////

            ViewBag.Dates = topDate.Select(d => d.Date).Distinct();
            ViewBag.latestDate = latestDate.Date.ToShortDateString();            

            return View(topTitles);
        }

Code for Index.cshtml

@model IEnumerable<App.Models.TopSong>
@{
    ViewBag.Title = "Top Songs";
    var songs = ViewBag.songs;
}            
<h1>@ViewBag.Message</h1>

<h2>Song's Updated On: @ViewBag.latestDate</h2>
@Html.ActionLink("Previous Week's Songs", "Index", new { date=@ViewBag.PreviousDate });
@Html.ActionLink("Next Week's Songs", "Index", new { date=@ViewBag.NextDate });

Basically i just want to be able to go to previous or next date using action links. I tried above code but it is not working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:42:18+00:00Added an answer on June 10, 2026 at 8:42 am

    One way of doing this is as follows:

    First, create an action method

    Public ActionResult Songs(string val)
    {
       if (val == "Previous")
       {
        // code to get and return previous week songs
       }
    
       if (val == "Next")
       {
        // code to get and return next week songs
       }
    
        // By default return either previous/next/current week's songs...
    }
    

    Second, modify your action links to match as below

    @Html.ActionLink("Previous Week's songs", "Songs", new { val = "Previous"})
    
    @Html.ActionLink("Next Week's Songs", "Songs", new { val = "Next"})
    

    Another Approach:

    Another approach is to create an action method for each, that means you can have an action method to just return Previous Week’s Songs and another action method to return Next Week’s songs. This means you will not need to pass any arguments and your actionlink code will look as simple as below

       @Html.ActionLink("Previous Week's songs", "PreviousWeekSongs")
    
       @Html.ActionLink("Next Week's Songs", "NextWeekSongs")
    

    UPDATE:

    Hey it seems like you do not have any code specific to binding your model data back to the View…so try updating your View code as below

        @model IEnumerable<App.Models.TopSong>
    @{
        ViewBag.Title = "Top Songs";
        var songs = ViewBag.songs;
    }            
    <h1>@ViewBag.Message</h1>
    // Code to Display Songs
    @foreach(var song in model)
    {
      <div> 
       <span>@song.Property1</span>
       <span>@song.Property2</span>
       <span>@song.Property3</span>          
      </div>
    
    }
    
    <h2>Song's Updated On: @ViewBag.latestDate</h2>
    @Html.ActionLink("Previous Week's Songs", "Index", new { date=@ViewBag.PreviousDate });
    @Html.ActionLink("Next Week's Songs", "Index", new { date=@ViewBag.NextDate });
    

    I hope this may help you…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in controller: public ActionResult Index(string date) { var topsong =
I have the following code in my HomeController: public ActionResult Create() { return View();
I have the following action: public class HomeController : Controller { public ActionResult Index(int?
I have the following code in my HomeController's Index action, and the code below
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code that I am compiling in a .NET 4.0 project namespace
I have the following code that populates two drop down lists, it all works
With the following code (using Moq 4.0.10501.6): HomeController controller = new HomeController(); ActionResult result
I have following code: public abstract class Operand<T> { public T Value { get;
I have following code snipped: ... var tpc = new ThirtPartyClass(); tpc.ExecuteCommand(); tpc.ExecuteCommand(); ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.