asp.net mvc - Is it possible to save a variable in controller -


i save variable in controller able use methods declared 3 private strings

public class bankaccountcontroller : controller {      private string datef, datet, accid;     //controller methods } 

now method changes values:

[httppost] public actionresult filter(string datefrom, string dateto, string accountid) {      datef = datefrom;      datet = dateto;      accid = accountid;      //rest of code } 

i used breakpoint , variables being changed when call controller method, when call other controller methods such these below private strings being reset emtpy strings, how can prevent happening?

public actionresult print()         {             return new actionaspdf(                 "printfilter", new { datefrom = datef, dateto = datet, accountid = accid }) { filename = "account transactions.pdf" };         }      public actionresult printfilter(string datefrom, string dateto, string accountid)     {             commonlayer.account acc = businesslayer.accountmanager.instance.getaccount(convert.toint16(accid));             viewbag.account = businesslayer.accountmanager.instance.getaccount(convert.toint16(accountid));             viewbag.selectedaccount = convert.toint16(accountid);             list<commonlayer.transaction> trans = businesslayer.accountmanager.instance.filter(convert.todatetime(datefrom), convert.todatetime(dateto), convert.toint16(accountid));             viewbag.transactions = trans;             return view(businesslayer.accountmanager.instance.getaccount(convert.toint16(accountid)));     } 

every request make new instance of controller created, therefore you're data not shared between requests. there's few things can save data:

session["datef"] = new datetime(); // save in session, (tied user) httpcontext.application["datef"] = new datetime(); // save in application (shared users) 

you can retrieve values in same way. of course, save somewhere else, bottom point is, controller-instances not shared, need save somewhere else.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -