Ejemplo n.º 1
0
        public ActionResult Create(tblNew tblnews, FormCollection Collection, string id, int[] MutilMenu)
        {
            if ((Request.Cookies["Username"] == null))
            {
                return(RedirectToAction("LoginIndex", "Login"));
            }


            string nidCate = Collection["drMenu"];

            if (nidCate != "")
            {
                tblnews.idCate = int.Parse(nidCate);
                int idcate = int.Parse(nidCate);
                tblnews.DateCreate = DateTime.Now;
                tblnews.Tag        = StringClass.NameToTag(tblnews.Name);
                tblnews.DateCreate = DateTime.Now;
                string idUser = Request.Cookies["Username"].Values["UserID"];
                tblnews.idUser = int.Parse(idUser);
                StringClass.NewsCreateTag(tblnews.id, tblnews.Keyword);
                db.tblNews.Add(tblnews);
                db.SaveChanges();
                var listprro = db.tblNews.OrderByDescending(p => p.id).Take(1).ToList();
                clsSitemap.CreateSitemap("tin-tuc/" + tblnews.Tag, listprro[0].id.ToString(), "News");
                #region [Updatehistory]
                Updatehistoty.UpdateHistory("Create News", Request.Cookies["Username"].Values["Username"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString());
                #endregion
                var ListNews = db.tblNews.OrderByDescending(p => p.id).Take(1).ToList();

                int idNews = int.Parse(ListNews[0].id.ToString());
                if (MutilMenu != null)
                {
                    foreach (var idCate in MutilMenu)
                    {
                        tblConnectNew tblconnectnews = new tblConnectNew();
                        tblconnectnews.idCate = idCate;
                        tblconnectnews.idNew  = idNews;
                        db.tblConnectNews.Add(tblconnectnews);
                        db.SaveChanges();
                    }
                }
                if (Collection["btnSave"] != null)
                {
                    Session["Thongbao"] = "<div  class=\"alert alert-info alert1\">Bạn đã thêm tinthành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>";
                    return(Redirect("/Newsad/index?idCate=" + nidCate + ""));
                }
                if (Collection["btnSaveCreate"] != null)
                {
                    Session["Thongbao"] = "<div  class=\"alert alert-info\">Bạn đã thêm tin  thành công, mời bạn thêm tin mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>";
                    return(Redirect("/Newsad/Create?id=" + nidCate + ""));
                }
            }
            return(View());
        }
Ejemplo n.º 2
0
        public ActionResult Edit(tblNew tblnews, FormCollection collection, int?id, int[] MutilMenu)
        {
            if (ModelState.IsValid)
            {
                if (collection["drMenu"] != "" || collection["drMenu"] != null)
                {
                    string idUser = Request.Cookies["Username"].Values["UserID"];
                    tblnews.idUser = int.Parse(idUser);

                    bool URL = (collection["URL"] == "false") ? false : true;
                    if (URL == true)
                    {
                        tblnews.Tag = StringClass.NameToTag(tblnews.Name);
                    }
                    else
                    {
                        tblnews.Tag = collection["NameURL"];
                    }
                    clsSitemap.CreateSitemap(tblnews.Tag + ".html", id.ToString(), "Newsad");
                    tblnews.idCate          = int.Parse(collection["drMenu"]);
                    tblnews.DateCreate      = DateTime.Now;
                    db.Entry(tblnews).State = EntityState.Modified;
                    db.SaveChanges();

                    if (URL == true)
                    {
                        tblnews.Tag = StringClass.NameToTag(tblnews.Name);
                        clsSitemap.UpdateSitemap("tin-tuc/" + StringClass.NameToTag(tblnews.Name), id.ToString(), "News");
                    }
                    else
                    {
                        tblnews.Tag = collection["NameURL"];
                        clsSitemap.UpdateSitemap("tin-tuc/" + StringClass.NameToTag(tblnews.Name), id.ToString(), "News");
                    }
                    int Ord     = int.Parse(tblnews.Ord.ToString());
                    int idCate  = int.Parse(collection["drMenu"]);
                    var Kiemtra = db.tblNews.Where(p => p.Ord == Ord && p.idCate == idCate && p.id != id).ToList();
                    if (Kiemtra.Count > 0)
                    {
                        var ListNewss = db.tblNews.Where(p => p.Ord >= Ord && p.idCate == idCate).ToList();
                        for (int i = 0; i < ListNewss.Count; i++)
                        {
                            int idp       = int.Parse(ListNewss[i].id.ToString());
                            var NewUpdate = db.tblNews.Find(idp);
                            NewUpdate.Ord = NewUpdate.Ord + 1;
                            db.SaveChanges();
                        }
                    }
                    db.SaveChanges();
                    var ListNews = db.tblConnectNews.Where(p => p.idNew == id).ToList();
                    for (int i = 0; i < ListNews.Count; i++)
                    {
                        db.tblConnectNews.Remove(ListNews[i]);
                        db.SaveChanges();
                    }
                    if (MutilMenu != null)
                    {
                        foreach (var idCates in MutilMenu)
                        {
                            tblConnectNew tblconnectnews = new tblConnectNew();
                            tblconnectnews.idCate = idCates;
                            tblconnectnews.idNew  = id;
                            db.tblConnectNews.Add(tblconnectnews);
                            db.SaveChanges();
                        }
                    }
                }
                if (collection["btnSave"] != null)
                {
                    Session["Thongbao"] = "<div  class=\"alert alert-info alert1\">Bạn đã sửa tin thành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>";

                    return(Redirect("/Newsad/Index?idCate=" + int.Parse(collection["drMenu"])));
                }
                if (collection["btnSaveCreate"] != null)
                {
                    Session["Thongbao"] = "<div  class=\"alert alert-info\">Bạn đã thêm tin thành công, mời bạn thêm tin mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>";
                    return(Redirect("/Newsad/Create?id=" + +int.Parse(collection["drMenu"]) + ""));
                }
                #region [Updatehistory]
                Updatehistoty.UpdateHistory("Update News", Request.Cookies["Username"].Values["Username"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString());
                #endregion
            }
            return(View(tblnews));
        }