Static method and TempData, what is another alternative?

yguyon28
yguyon28 used Ask the Experts™
on
I'm trying to find  a way to pass  a TempData from a static method to another method but I'm having some issue finding the correct answer to this problem bellow, since I can't use a TempData in a static field..

   
 public static XtraReport BuildReportObject(DailyEmployeeRecapSearchCriteria searchCriteria)
        {
           var resultsContainer = GetReportResults(searchCriteria)
           TempData['"value"] = resultsContainer;  I can't do this, since its a static field.
          ........

          return SetDefaultsForReportObject(mainReport, .....
        }

why I wanted to use a TempData so that I could use it into another method.

 public FileResult ExportCsvFormat(string dataSource)
        {
  foreach (var x in (List<GroupBillingCustomerDetails>)TempData["GroupBillingdata"])  > Then here I can use my TempData ...
                {

}.....
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017
Commented:
Hi yguyon28;

you could make the variable a static field itself. Not knowing the data type of TempData I can not say specifically but here is one using an int array defined at class level.

static int[] TempData = new int[10] ;

Open in new window

Senior .Net Developer
Commented:
For Fernando:

TempData is an MVC concept similar to session: https://stackoverflow.com/questions/313572/what-is-tempdata-collection-used-for-in-asp-net-mvc


I would pass in a reference string which you can then call in your main controller:

eg:
public static XtraReport BuildReportObject(DailyEmployeeRecapSearchCriteria searchCriteria, ref Object reportValue )
{
          var resultsContainer = GetReportResults(searchCriteria)
          reportValue = resultsContainer;
}

Open in new window


from there you could set the TempData in the calling object.

Another way (not tested, from: https://stackoverflow.com/questions/34207674/using-tempdata-outside-controller)
var tempDataDictionaryFactory = System.Web.HttpContext.Current.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
var tempDataDictionary = tempDataDictionaryFactory.GetTempData(context);
tempDataDictionary["value"] = resultsContainer

Open in new window

Fernando SotoRetired
Distinguished Expert 2017
Commented:
@yguyon28 is this a Web app?
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
You could take TempData in as a parameter. Then just reference it accordingly:

public static XtraReport BuildReportObject(DailyEmployeeRecapSearchCriteria searchCriteria, System.Web.Mvc.TempDataDictionary tempData)
{
    var resultsContainer = GetReportResults(searchCriteria)
    tempData['"value"] = resultsContainer;  I can't do this, since its a static field.
    ........

    return SetDefaultsForReportObject(mainReport, .....
}

Open in new window


In your controller:

var result = BuildReportObject(someParameter, TempData);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial