Asp.net mvc 是否可以在控制器中保存变量

Asp.net mvc 是否可以在控制器中保存变量,asp.net-mvc,model-view-controller,controller,Asp.net Mvc,Model View Controller,Controller,我想在控制器中保存一个变量,以便能够对所有方法使用它,所以我声明了3个私有字符串 public class BankAccountController : Controller { private string dateF, dateT, accID; //controller methods } 现在,此方法将更改其值: [HttpPost] public ActionResult Filter(string dateFrom, string dateTo, string

我想在控制器中保存一个变量,以便能够对所有方法使用它,所以我声明了3个私有字符串

public class BankAccountController : Controller
{
     private string dateF, dateT, accID;
    //controller methods
}
现在,此方法将更改其值:

[HttpPost]
public ActionResult Filter(string dateFrom, string dateTo, string accountid)
{
     dateF = dateFrom;
     dateT = dateTo;
     accID = accountid;
     //rest of the code
}
我使用了一个断点,当我调用该控制器方法时,变量正在更改,但是当我调用其他控制器方法(例如下面的这些方法)时,私有字符串将重置为emtpy字符串,如何防止这种情况发生

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)));
    }
public ActionResult Print()
{
返回新操作ASPDF(
“PrintFilter”,新的{dateFrom=dateF,dateTo=dateT,accountid=accID}{FileName=“Account Transactions.pdf”};
}
公共ActionResult打印筛选器(字符串dateFrom、字符串dateTo、字符串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 trans=BusinessLayer.AccountManager.Instance.filter(Convert.ToDateTime(dateFrom)、Convert.ToDateTime(dateTo)、Convert.ToInt16(accountid));
ViewBag.Transactions=trans;
返回视图(BusinessLayer.AccountManager.Instance.getAccount(Convert.ToInt16(accountid));
}

您发出的每个请求都将创建控制器的新实例,因此您的数据不会在请求之间共享。要保存数据,您可以做以下几件事:

Session["dateF"] = new DateTime(); // save it in the session, (tied to user)
HttpContext.Application["dateF"] = new DateTime(); // save it in application (shared by all users)

您可以用同样的方法检索这些值。当然,您也可以将其保存到其他地方,最重要的是,控制器实例不共享,您需要将其保存到其他地方。

以下方法非常简单,确保变量绑定到当前用户,而不是在整个应用程序中使用。您只需在控制器中键入以下代码:

Session["dateF"] = dateFrom;
Session["dateT"] = dateTo;
Session["accID"] = accountid;
当您想使用该变量时,例如,您想将其作为方法的参数,只需键入以下内容:

MyMethod(Session["dateF"].ToString());

这就是在ASP.NET MVC中保存和使用变量的方式。您可以在控制器中使用静态字段,以便在所有请求之间共享

私有静态列表yourObjectList