如何在mvc视图中定义空会话,c#

时间:2016-03-29 13:15:41

标签: c# asp.net-mvc session

我已在视图中定义并清空session,如此

<input id="myc" type="hidden" data-value="@Request.RequestContext.HttpContext.Session["Ealert"].ToString()" />

但是当我运行时,它表示对象引用未设置为对象的实例。

我不知道。在我的控制器中我喜欢这个

Session["Ealert"] = null;

            Random random = new Random();
            int idONe = random.Next(99,999);
            int idTwo = random.Next(999, 9999);
            string middle = "menuka";

            string fullID = idONe.ToString() + middle + idTwo.ToString();

            var N_ID = fullID;
            var N_Email = N_EmailAdd;



            TourCenterDBEntities NewsLetterEntities = new TourCenterDBEntities();

            var existing = NewsLetterEntities.News_Letter.Where(l => l.N_Email == N_EmailAdd);
            Debug.WriteLine(existing.Count());

            if(existing.Count()==0)
            {
                News_Letter NewsLetterDetails = new News_Letter();



                NewsLetterDetails.N_id = N_ID;
                NewsLetterDetails.N_Email = N_Email;

                NewsLetterEntities.News_Letter.Add(NewsLetterDetails);

                NewsLetterEntities.SaveChanges();

                Session["Ealert"] = "s";
            }
            else
            {
                Session["Ealert"] = "e";


            }


            return Json(new { });

我怎么能这样做。给你这个帮助。我完全陷入困境。

1 个答案:

答案 0 :(得分:0)

好吧试试这个

@if(Request.RequestContext.HttpContext.Session["Ealert"]!=null)
{
<input id="myc" type="hidden" data-value="@Request.RequestContext.HttpContext.Session["Ealert"].ToString()" />

}