1

in my project cookies value not set,i can't understand why,where the problem i cant find out .please help

 [HttpPost]
    public ActionResult sellerLogin(string userid,string password)
    {

        int count = db.sellers.Count(p => p.userId == userid && p.password == password);
        if (count == 1)
        {
            HttpCookie selleruserid = new HttpCookie("selleruserid");
            selleruserid["selleruserid"] = userid.ToString();
            selleruserid.Expires = DateTime.Now.AddDays(220);
            Response.Cookies.Add(selleruserid);                
            return View("loginSuccess");
        }


        return View();
    }

loginSuccess() Action result is

 public ActionResult loginSuccess()
    {
        HttpCookie selleruserid = Request.Cookies["selleruserid"];
        if (selleruserid != null)
        {
            ViewBag.userid = selleruserid["selleruserid"].ToString();

        } 


        return View();
    }

loginSuccess() View contain

@ViewBag.userid

but when control goes to loginsuccess then viewbag showing blank...

Sk Asraf
  • 163
  • 2
  • 14
  • 1
    Possible duplicate of [Using Cookie in Asp.Net Mvc 4](http://stackoverflow.com/questions/19128507/using-cookie-in-asp-net-mvc-4) – Dr. Stitch Jun 16 '16 at 05:47

2 Answers2

0

Try using Response.SetCookie(), because Response.Cookie.Add() can cause multiple cookies to be added, whereas SetCookie will update an existing cookie.

Better if you add logic to check if cookie already exists then Response.SetCookie else Response.Cookie.Add

Sami
  • 3,686
  • 4
  • 17
  • 28
0

Hi your code working fine for me, if you are using viewbag and any redirection occurred then your viewbag will be null, you can also use this Response.SetCookie(selleruserid);

chirag p
  • 31
  • 1
  • 4