【问题标题】:Session variables are not able to access in other controllers in asp.net mvc 5.0会话变量无法在 asp.net mvc 5.0 中的其他控制器中访问
【发布时间】: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


【解决方案1】:

您需要重新分配回 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();

【讨论】:

  • 非常感谢。它现在工作正常。从早上开始,我一直在努力寻找答案。
  • @Niranjangodbole 如果它解决了您的问题,请让它接受答案
猜你喜欢
  • 2015-06-10
  • 2021-04-15
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 2019-12-03
  • 2013-05-28
相关资源
最近更新 更多