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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:13:38+00:00 2026-05-25T23:13:38+00:00

Simplified example: [HttpGet] public ActionResult Report(DateTime? date) { if (!date.HasValue) { date = DateTime.Now;

  • 0

Simplified example:

[HttpGet]
public ActionResult Report(DateTime? date)
{
    if (!date.HasValue)
    {
        date = DateTime.Now;
    }

    // Clear the ModelState so that the date is displayed in the correct format as specified by the DisplayFormat attribute on the model
    ModelState.Clear();

    // Expensive call to database to retrieve the report data
    ReportModel model = DataAdapter.Convert(this.reportService.GetReport((DateTime)date));

    // Store in TempData in case validation fails on HttpPost
    TempData["ReportModel"] = model;

    return View(model);
}

[HttpPost]
public ActionResult Report(ReportModel model)
{
    if (ModelState.IsValid)
    {
        return RedirectToAction("Report", new { date = model.Date });
    }
    else
    {
        // Load the report from TempData, and restore again in case of another validation failure
        model = TempData["ReportModel"] as ReportModel;
        TempData["ReportModel"] = model;

        // Redisplay the view, the user inputted value for date will be loaded from ModelState
        return View(model);
    }
}

My question is, am I going about this the right way by storing the report data in TempData? The code seems a little strange especially reading from and then writing back to TempData in the HttpPost action method to ensure it persists the next request.

Other options I can think of are:

(1) Make another call to the service layer from the HttpPost action (I’d rather not make another database call because of a validation failure just to redisplay the form as it seems inefficient). I guess I could implement caching at the service layer to avoid the database round trip…

(2) Use hidden inputs in the form (yuk!).

(3) Store the most recently viewed report in session permanently.

How is everyone else doing this sort of thing? What’s the recommended practice?

  • 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-05-25T23:13:39+00:00Added an answer on May 25, 2026 at 11:13 pm

    My question is, am I going about this the right way by storing the report data in TempData?

    No, absolutely not. Store something into TempData if and only if you redirect immediately afterwards as TempData survivces only a single redirect. If you store something into TempData in your GET action and then render a view, an AJAX request for example from this view would kill the TempData and you won’t get the values back on your POST request.

    The correct pattern is the following (no TempData whatsoever):

    public ActionResult Report(DateTime? date)
    {
        if (!date.HasValue)
        {
            date = DateTime.Now;
        }
    
        // Clear the ModelState so that the date is displayed in the correct format as specified by the DisplayFormat attribute on the model
        ModelState.Clear();
    
        // Expensive call to database to retrieve the report data
        ReportModel model = DataAdapter.Convert(this.reportService.GetReport((DateTime)date));
    
        return View(model);
    }
    
    [HttpPost]
    public ActionResult Report(ReportModel model)
    {
        if (ModelState.IsValid)
        {
            return RedirectToAction("Report", new { date = model.Date });
        }
        else
        {
            // Redisplay the view, the user inputted value for date will be loaded from ModelState
            // TODO: query the database/cache to refetch any fields that weren't present
            // in the form and that you need when redisplaying the view
            return View(model);
        }
    }
    

    (1) Make another call to the service layer from the HttpPost action
    (I’d rather not make another database call because of a validation
    failure just to redisplay the form as it seems inefficient). I guess I
    could implement caching at the service layer to avoid the database
    round trip…

    That’s exactly what you should do. And if you have problems with optimizing those queries or concerns of hitting your or something on each POST request cache those results. Databases are hyper optimized nowadays and are designed to do exactly this (don’t abuse of course, define your indexes on proper columns and performance should be good). But of course caching is the best way to avoid hitting the database if you have a very demanding web site with lots of requests and users.

    (2) Use hidden inputs in the form (yuk!).

    Yuk, I agree but could work in situations where you don’t have lots of them.

    (3) Store the most recently viewed report in session permanently.

    No, avoid Session. Session is the enemy of scalable and stateless applications.

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

Sidebar

Related Questions

As a simplified example, I have the following data classes: public class Employee {
Simplified example: I have an object that models a user. Users have a first
Imagine you live in very simplified example land - and imagine that you've got
I have an order entry page and a corresponding ViewModel (simplified example): public class
I have an HTML table that is similar to this simplified example: <table> <tbody>
The following code (simplified example, that triggers the error) doesn't compile with VS 2008:
This is a simplified example, but I am working on a code translator that
In this simplified example I have a generic class, and a method that returns
I have a class (simplified example) like : public class SomeCollection : ICloneable {
This is a simplified example, to make sure that there are no asynchronous fading/animation

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.