简体   繁体   中英

How can I define empty session in mvc view, c#

I have define and empty session inside the view like this

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

but when I run, it says Object reference not set to an instance of an object.

I have no idea.in my controller I do like this

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 { });

how can I do it.hope your help with this.i'm full of stuck.

ok try this

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM