public ActionResult Edit(FormCollection collection, int id)
        {
            string   title      = collection["title"].ToString();
            string   summary    = collection["summary"].ToString();
            string   content    = collection["content"].ToString();
            DateTime dateposted = DateTime.Parse(collection["dateposted"].ToString());
            int      authorid   = int.Parse(collection["authorid"].ToString());
            int      posterid   = int.Parse(collection["posterid"].ToString());
            string   avatar     = collection["avatar"].ToString();
            int      categoryid = int.Parse(collection["categoryid"].ToString());
            string   trangthai  = collection["trangthai"].ToString();

            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                News news = db.News.SingleOrDefault(n => n.Id == id);
                news.Title      = title;
                news.Summary    = summary;
                news.Content    = content;
                news.Dateposted = dateposted;
                news.AuthorId   = authorid;
                news.PosterId   = posterid;
                news.Avatar     = avatar;
                news.CategoryId = categoryid;
                news.TrangThai  = trangthai;

                ViewBag.PosterId   = new SelectList(db.Accounts.ToList(), "Id", "Fullname", news.PosterId);
                ViewBag.AuthorId   = new SelectList(db.Authors.ToList(), "Id", "Name", news.AuthorId);
                ViewBag.CategoryId = new SelectList(db.Categories.Where(n => !(n.Url == "" && n.ParentId == null)).ToList(), "Id", "Name", news.CategoryId);
                db.SaveChanges();

                return(RedirectToAction("Create"));
            }
        }
Esempio n. 2
0
        public ActionResult Signup(FormCollection collection)
        {
            string fullname     = collection["fullname"].ToString();
            string email        = collection["email"].ToString();
            string password     = collection["password"].ToString();
            string confpassword = collection["confpassword"].ToString();
            string avatar       = collection["avatar"].ToString();
            string trangthai    = "Hoạt động";
            string quyen        = "User";

            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                Account acc        = new Account();
                var     checkEmail = db.Accounts.SingleOrDefault(n => n.Email.Equals(email));
                if (checkEmail == null)
                {
                    acc.Fullname        = fullname;
                    acc.Email           = email;
                    acc.Password        = password;
                    acc.Confirmpassword = confpassword;
                    acc.Avatar          = avatar;
                    acc.TrangThai       = trangthai;
                    acc.Quyen           = quyen;
                    db.Accounts.Add(acc);
                    db.SaveChanges();
                    ViewBag.tbDangKy = acc.Fullname + "  đăng ký thành công!";
                }
                else
                {
                    ViewBag.tbDangKyLoi = email + " đã tồn tại!";
                }
                return(View());
            }
        }
        public ActionResult Edit(FormCollection collection, int id)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                string   nameCategory     = collection["namecategory"].ToString();
                string   urlCategory      = collection["urlcategory"].ToString();
                string   parentIdCategory = collection["ParentId"].ToString();
                Category cate             = db.Categories.SingleOrDefault(n => n.Id == id);
                ViewBag.ParentId = new SelectList(db.Categories.Where(n => n.ParentId == null), "Id", "Name", cate.ParentId).ToList();
                cate.Name        = nameCategory;

                if (parentIdCategory == "NULL")
                {
                    cate.ParentId = null;
                }
                else
                {
                    cate.ParentId = Convert.ToInt32(parentIdCategory);
                }

                cate.Url = urlCategory;

                db.SaveChanges();
                return(RedirectToAction("Create"));
            }
        }
        public ActionResult Detail(int id = 0)
        {
            News news = db.News.SingleOrDefault(n => n.Id == id);

            news.View = news.View + 1;
            db.SaveChanges();
            if (news == null)
            {
                Response.StatusCode = 404;
                return(null);
            }
            return(View(news));
        }
        public ActionResult Delete(int id)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                News news = db.News.SingleOrDefault(n => n.Id == id);

                db.News.Remove(news);

                db.SaveChanges();

                return(RedirectToAction("Create"));
            }
        }
        public ActionResult Edit(FormCollection collection, int id)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                string authorName    = collection["authorname"].ToString();
                string authorAddress = collection["authoraddress"].ToString();
                Author aut           = db.Authors.SingleOrDefault(n => n.Id == id);
                aut.Name    = authorName;
                aut.Address = authorAddress;

                db.SaveChanges();
                return(RedirectToAction("Create"));
            }
        }
        public ActionResult Delete(int id)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                //lay doi tuong can xoa
                Author aut = db.Authors.SingleOrDefault(n => n.Id == id);

                // thuc hien xoa doi tuong
                db.Authors.Remove(aut);

                //Thay doi trong csdl
                db.SaveChanges();
                return(RedirectToAction("Create"));
            }
        }
Esempio n. 8
0
 // POST: //Account/DelAccount/
 public int DelAccount(int id)
 {
     using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
     {
         Account delaccount = db.Accounts.SingleOrDefault(n => n.Id == id);
         if (delaccount != null)
         {
             db.Accounts.Remove(delaccount);
             db.SaveChanges();
             return(id);
         }
         else
         {
             return(-1);
         }
     }
 }
 public string ChangeImage(int id, string avatar)
 {
     using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
     {
         if (id == 0)
         {
             return("Mã không tồn tại");
         }
         News news = db.News.Find(id);
         if (news == null)
         {
             return("Mã không tồn tại");
         }
         news.Avatar = avatar;
         db.SaveChanges();
         return("");
     }
 }
        public ActionResult Create(FormCollection collection)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                string authorName    = collection["authorname"].ToString();
                string authorAddress = collection["authoraddress"].ToString();

                Author aut = new Author();
                aut.Name    = authorName;
                aut.Address = authorAddress;

                db.Authors.Add(aut);
                db.SaveChanges();

                List <Author> lstAuthor = db.Authors.ToList();
                return(View(lstAuthor));
            }
        }
Esempio n. 11
0
        public ActionResult Create(FormCollection collection, int?page)
        {
            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            string   title      = collection["title"].ToString();
            string   summary    = collection["summary"].ToString();
            string   content    = collection["content"].ToString();
            DateTime dateposted = DateTime.Parse(collection["dateposted"].ToString());
            int      authorid   = int.Parse(collection["authorid"].ToString());
            int      posterid   = int.Parse(collection["posterid"].ToString());
            string   avatar     = collection["avatar"].ToString();
            string   trangthai  = collection["trangthai"].ToString();
            int      view       = 0;
            int      categoryid = int.Parse(collection["categoryid"].ToString());

            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                ViewBag.PosterId   = new SelectList(db.Accounts.ToList(), "Id", "Fullname");
                ViewBag.AuthorId   = new SelectList(db.Authors.ToList(), "Id", "Name");
                ViewBag.CategoryId = new SelectList(db.Categories.Where(n => !(n.Url == "" && n.ParentId == null)).ToList().ToList(), "Id", "Name");

                News news = new News();
                news.Title      = title;
                news.Summary    = summary;
                news.Content    = content;
                news.Dateposted = dateposted;
                news.AuthorId   = authorid;
                news.PosterId   = posterid;
                news.Avatar     = avatar;
                news.CategoryId = categoryid;
                news.TrangThai  = trangthai;
                news.View       = view;
                db.News.Add(news);
                db.SaveChanges();
                return(View(db.News.ToList().ToPagedList(pageNumber, pageSize)));
            }
        }
        public ActionResult Create(FormCollection collection)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                string   nameCategory     = collection["namecategory"].ToString();
                string   urlCategory      = collection["urlcategory"].ToString();
                string   parentIdCategory = collection["ParentId"].ToString();
                Category cate             = new Category();
                cate.Name = nameCategory;
                if (urlCategory == null)
                {
                    cate.Url = null;
                }
                else
                {
                    cate.Url = urlCategory;
                }

                if (parentIdCategory == "NULL")
                {
                    cate.ParentId = null;
                }
                else
                {
                    cate.ParentId = Convert.ToInt32(parentIdCategory);
                }

                //Them doi tuong vao csdl
                db.Categories.Add(cate);
                //Thuc hien ghi vao csdl
                db.SaveChanges();
                ViewBag.ParentId = new SelectList(db.Categories.Where(n => n.ParentId == null), "Id", "Name").ToList();
                //Lay danh sach chuyen muc trong csdl tra ve view
                List <Category> lstCategory = db.Categories.ToList();
                return(View(lstCategory));
            }
        }
        public ActionResult Delete(int id)
        {
            using (Web_NEWS_MVCEntities db = new Web_NEWS_MVCEntities())
            {
                try
                {
                    //Lấy đối tượng cần xóa
                    Category cate = db.Categories.SingleOrDefault(x => x.Id == id);

                    //Thực hiện xóa đối tượng
                    db.Categories.Remove(cate);

                    //Thực hiện thay đổi trong csdl
                    db.SaveChanges();

                    //Lay danh sach chuyen muc trong csdl tra ve view
                    return(RedirectToAction("Create"));
                }
                catch
                {
                    return(RedirectToAction("Create"));
                }
            }
        }