在ASP.NET MVC中,一個在方法外部聲明的字段,該字段每次都會初始化。如何爲MVC Controller中的每個客戶端初始化一次字段?
那裏我給出了一個演示例子。
public class TestController : Controller
{
private List<object> ListItems;
// GET: /Test/Index
public ActionResult Index(int Id)
{
ModelSource _model = new ModelSource(Id);
ListItems = _model.GetItems();//coming info from Model
return View();
}
// GET: /Test/Demo
public ActionResult Demo()
{
int x = ListItems.Count;
//Do Something More
return View(x);
}
}
有可能是ListItems
將初始化一次爲每個客戶端作爲Id
參數將會對客戶的要求不同。
保存在'Session'中。 –
@PatrickHofman,首先,謝謝你的回覆,你能給我一些參考嗎? –
http://stackoverflow.com/q/14138872/993547 –