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 8445327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:37:47+00:00 2026-06-10T09:37:47+00:00

I have following code in controller: public ActionResult Index(string date) { var topsong =

  • 0

I have following code in controller:

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

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


           if (date == "PreviousDate")
            {
                latestDate = topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault();                
            }

            if (date == "NextDate")
            {
                latestDate = topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault();
            }           
.... 
return View();

}

Here is code in View:

@Html.ActionLink("Previous Week's Songs", "Index", new { date = "PreviousDate" }) <br />
@Html.ActionLink("Next Week's Songs", "Index", new { date = "NextDate" })

If user click on PreviousDate it is sent back to controller and it chooses the previous date. First time it works fine. Problem comes the second time. As latestDate is calculated again but it should basically use PreviousDate as latestDate not calculate latestDate again. Is there a way to persist PreviousDate? Or pass it back from the View?

Here is an example:
If latestDate is 08/27/2012 first time it is loaded when i press previousDate it loads date 08/25/2012 but when i press PreviousDate again instead of using 08/25/2012 as latestDate it again uses 08/27/2012 as latest date.

TopDate class:

public class Top10Date
    {
        [Key]
        public int DateId { get; set; }
        public DateTime Date { get; set; }        
    }
  • 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-10T09:37:48+00:00Added an answer on June 10, 2026 at 9:37 am

    You will want to pass the current date that is being shown as well as the “sub-action” of PreviousDate and NextDate back to the controller.

    In the controller:

        public ActionResult Index(string date, DateTime? latestDate)
        {
            var topsong = db.TopSongs;
            var topDate = db.TopDates;
    
            if (!latestDate.HasValue)
                latestDate = (DateTime?)(topDate.OrderByDescending(d => d.Date).FirstOrDefault());
    
    
           if (date.Value == "PreviousDate")
            {
                latestDate = (DateTime?)(topDate.Where(d => d.Date < latestDate.Date).OrderByDescending(d => d.Date).FirstOrDefault());                
            }
    
            if (date.Value == "NextDate")
            {
                latestDate = (DateTime?)(topDate.Where(d => d.Date > latestDate.Date).OrderBy(d => d.Date).FirstOrDefault());
            }
    
            .... 
    
            ViewBag.latestDate = latestDate
    
            return View();
        }
    

    And then

    @Html.ActionLink("Previous Week's Songs", "Index", new { date = "PreviousDate", ViewBag.latestDate }) <br />
    @Html.ActionLink("Next Week's Songs", "Index", new { date = "NextDate", ViewBag.latestDate })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
I have the following action: public class HomeController : Controller { public ActionResult Index(int?
I have the following code in my controller: // GET: /Review/Create public ActionResult Create()
Hi there I have the following action in my controller [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index()
I have a controller class as in the following code segment: @Controller public class
i'm new to routing in aspnet mvc.. i have following code: Action Controller public
I have the following code public class BaseController : Controller { SqlConnection con; DataSet
I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
In Symfony 2 I have the following code in my Controller: // prepare to
I have the following code in my gsp <select onChange=${remoteFunction(action:'superior', controller:'employee', params: '\'name=\' +

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.