public ActionResult Command() { var listNews = db.TblNews.ToList(); for (int i = 0; i < listNews.Count; i++) { TblConnectManuToNews connect = new TblConnectManuToNews(); connect.IdManu = 1; connect.IdNews = listNews[i].Id; db.TblConnectManuToNews.Add(connect); db.SaveChanges(); } return(View()); }
public ActionResult Edit(TblNews TblNews, FormCollection collection, int?id, int[] mutilManu) { 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, 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(StringClass.NameToTag(TblNews.Name) + ".htm", id.ToString(), "News"); } else { TblNews.Tag = collection["NameURL"]; clsSitemap.UpdateSitemap(StringClass.NameToTag(TblNews.Name) + ".htm", 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 listNewsTag = db.TblNewsTag.Where(p => p.Idn == id).ToList(); for (int i = 0; i < listNewsTag.Count; i++) { int ids = listNewsTag[i].Id; TblNewsTag TblNewstags = db.TblNewsTag.Find(ids); db.TblNewsTag.Remove(TblNewstags); db.SaveChanges(); } string nkeyword = collection["Tabs"]; string[] mangKeyword = nkeyword.Split(','); for (int i = 0; i < mangKeyword.Length; i++) { if (mangKeyword[i] != null && mangKeyword[i] != "") { TblNewsTag TblNewstags = new TblNewsTag(); TblNewstags.Idn = id; TblNewstags.Name = mangKeyword[i]; TblNewstags.Tag = StringClass.NameToTag(mangKeyword[i]); db.TblNewsTag.Add(TblNewstags); db.SaveChanges(); } } var listIdManu = db.TblConnectManuToNews.Where(p => p.IdNews == id).ToList(); for (int i = 0; i < listIdManu.Count; i++) { db.TblConnectManuToNews.Remove(listIdManu[i]); db.SaveChanges(); } if (mutilManu != null) { foreach (var idMenu in mutilManu) { TblConnectManuToNews connectimage = new TblConnectManuToNews(); connectimage.IdManu = idMenu; connectimage.IdNews = id; db.TblConnectManuToNews.Add(connectimage); 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] #endregion } return(View(TblNews)); }
public ActionResult Create(TblNews TblNews, FormCollection Collection, string id, int[] mutilManu) { 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; TblNews.Visit = 0; string IdUser = Request.Cookies["Username"].Values["UserID"]; TblNews.IdUser = int.Parse(IdUser); db.TblNews.Add(TblNews); db.SaveChanges(); var ListNews = db.TblNews.OrderByDescending(p => p.Id).Take(1).ToList(); int IdNews = int.Parse(ListNews[0].Id.ToString()); string nkeyword = TblNews.Tabs; string[] mangKeyword = nkeyword.Split(','); for (int i = 0; i < mangKeyword.Length; i++) { if (mangKeyword[i] != null && mangKeyword[i] != "") { TblNewsTag TblNewstag = new TblNewsTag(); TblNewstag.Idn = IdNews; TblNewstag.Name = mangKeyword[i]; TblNewstag.Tag = StringClass.NameToTag(mangKeyword[i]); db.TblNewsTag.Add(TblNewstag); db.SaveChanges(); } } var listprro = db.TblNews.OrderByDescending(p => p.Id).Take(1).ToList(); clsSitemap.CreateSitemap(TblNews.Tag + ".htm", listprro[0].Id.ToString(), "News"); #region [Updatehistory] #endregion if (mutilManu != null) { foreach (var idMenu in mutilManu) { TblConnectManuToNews conntectNews = new TblConnectManuToNews(); conntectNews.IdManu = idMenu; conntectNews.IdNews = IdNews; db.TblConnectManuToNews.Add(conntectNews); 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()); }