[英]Session variable in ASP.NET MVC
I need to create something like a session variable in .net MVC.我需要在 .net MVC 中创建类似会话变量的东西。 I'm using ViewData.
我正在使用 ViewData。 First I set it in my Controller Index:
首先我在我的控制器索引中设置它:
public ActionResult Index(int id)
{
ViewData["myID"] = id;
}
Then, I'll need to use it in another function of the same controller.然后,我需要在同一个控制器的另一个函数中使用它。 However, I have to send it to my view.
但是,我必须将其发送给我的视图。 Store the data into a .
将数据存储到一个 . Then read it again.
然后再读一遍。 View:
看法:
<input type="hidden" id="myID" name="myID" value='@ViewData["myID"]' />
Other function of the controller where I get the id:我得到 id 的控制器的其他功能:
[HttpPost, ValidateInput(false)]
public ActionResult Test(FormCollection form)
{
var pId = form["myID"];
}
It works, but looks wrong (I'm new in .net mvc).它有效,但看起来不对(我是 .net mvc 的新手)。 Is there a way where I can set this id one time in my controller and then read/get it when I need?
有没有一种方法可以在我的控制器中设置一次此 ID,然后在需要时读取/获取它?
Thanks谢谢
try this试试这个
for set为集
System.Web.HttpContext.Current.Session["Id"] = 1;
for get为了得到
var id=Convert.ToInt32(System.Web.HttpContext.Current.Session["Id"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.