public IActionResult DeleteAd(int postId)
        {
            SimpleAdDb db = new SimpleAdDb();

            db.DeleteAd(postId);                                           //i don't know if it's necessary to remove the id from the cookie
            List <string> ids     = new List <string>();
            List <int>    postIds = new List <int>();

            if (Request.Cookies["postIds"] != null)               //if id is still on the cookie, it shouldn't do any harm
            {
                ids = Request.Cookies["postIds"].Split(',').ToList();
                foreach (string num in ids)
                {
                    postIds.Add(int.Parse(num));                        //filling a new list of ints with the string
                }
                postIds.Remove(postId);                                 //removing the deleted id
                string result = "";
                for (int i = 0; i < postIds.Count; i++)
                {
                    result += $"{postIds[i]}";                             //making a new string with no comma at the end
                    if (i < postIds.Count - 1)
                    {
                        result += ",";
                    }
                }
                Response.Cookies.Append("postIds", result);            //overriding the cookie with the new string
            }
            return(Redirect("/cookies/index"));
        }
Exemplo n.º 2
0
        public IActionResult DeleteAd(int postId)
        {
            SimpleAdDb db = new SimpleAdDb();

            db.DeleteAd(postId);
            if (HttpContext.Session.Get <List <int> >("adIds") != null)
            {
                HttpContext.Session.Get <List <int> >("adIds").Remove(postId);
            }
            return(Redirect("/home/index"));
        }