【发布时间】:2016-03-30 20:06:14
【问题描述】:
这是我的数据层类。
public class DataLayer
{
SqlConnection con = new SqlConnection("Data Source=AUFBKMDBSR1;DataBase=MVCDemo; Integrated Security=True;");
public bool select(MainModel mmdl)
{
bool flag = false;
con.Open();
string query = "select count(*) from SystemUsers where UserName='"+mmdl.UserName+"' and Password='"+mmdl.Password+"'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Connection = con;
flag = Convert.ToBoolean(cmd.ExecuteScalar());
return flag;
}
这是我的控制器。
public ActionResult Login(FormCollection frm)
{
//Creating object of model
//Accessing
mmdl.UserName = frm["UserName"];
mmdl.Password = frm["Password"];
DataLayer dl = new DataLayer();
if(dl.select(mmdl)==true)
{
//System.Web.HttpContext.Current.Session["UserName"] = mmdl; ;
Session["UserName"] = mmdl;
return RedirectToAction("Outlookhome", "Compose");
}
return View();
}
这是我无法访问 Session["UserName"] 的另一个控制器
public ActionResult ComposeEmail(FormCollection frm)
{
//creating compose class object to access properties in controller
Compose cmpse = new Compose();
cmpse.To= frm["To"];
cmpse.From = Session["UserName"].ToString(); ;
cmpse.Subject = frm["Subject"];
cmpse.DteTme = frm["DteTme"];
cmpse.Body = frm["Body"];
// cmpse.Attachement = frm["Attachement"];
DataLayer dl = new DataLayer();
//calling compose method from datalayer class to insert values into database.
if(dl.Compose(cmpse)==1)
{
}
return View();
}
cmpse.from 每次都返回空值。 我无法在所有控制器中使用会话变量。请帮忙...
【问题讨论】:
-
if(dl.select(mmdl)==true) 是否返回 true?
-
是的。它返回真实。除了 cmpse.From = Session["UserName"].ToString();
-
在帐户控制器中,我正在获取会话变量,并且我也能够在视图中显示会话变量。但是这个相同的会话变量我无法在其他控制器中检索或使用。
-
您正在将视图模型放入会话中,请尝试仅放入用户名
-
你在 web.config 中设置了 sessionTimeout 吗?
标签: c# asp.net-mvc session asp.net-mvc-5