public ActionResult Command() { var listNews = db.tblNews.ToList(); for (int i = 0; i < listNews.Count; i++) { tblConnectManuToNew connect = new tblConnectManuToNew(); connect.idManu = 1; connect.idNews = listNews[i].id; db.tblConnectManuToNews.Add(connect); db.SaveChanges(); } return(View()); }
public ActionResult Edit(tblNew 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("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 listNewsTag = db.tblNewsTags.Where(p => p.idn == id).ToList(); for (int i = 0; i < listNewsTag.Count; i++) { int ids = listNewsTag[i].id; tblNewsTag tblnewstags = db.tblNewsTags.Find(ids); db.tblNewsTags.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.tblNewsTags.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) { tblConnectManuToNew connectimage = new tblConnectManuToNew(); 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] Updatehistoty.UpdateHistory("Update News", Request.Cookies["Username"].Values["Username"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString()); #endregion } return(View(tblnews)); }
public ActionResult Create(tblNew 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.tblNewsTags.Add(tblnewstag); 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 if (mutilManu != null) { foreach (var idMenu in mutilManu) { tblConnectManuToNew conntectNews = new tblConnectManuToNew(); 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()); }