这是我的数据层类。
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();
}
这是我的其他控制器,我无法访问会话[" 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每次都返回null值。 我无法在所有控制器中使用会话变量。请帮忙...
答案 0 :(得分:0)
您需要重新分配回mmdl,其中mmdl是模型类的对象
//creating compose class object to access properties in controller
Compose cmpse = new Compose();
cmpse.To= frm["To"];
// assign back to mmdl object
var mmdl = Session["UserName"] as your model Class
cmpse.From = mmdl.UserName
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();