Static method and TempData, what is another alternative?

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 ...
                {

}.....
yguyon28Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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

Kyle AbrahamsSenior .Net DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
@yguyon28 is this a Web app?
käµfm³d 👽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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.