public ActionResult Create(SystemUser systemuser)
        {
            //檢查登入帳號是否重複
            if (db.SystemUser.Any(x => x.Account == systemuser.Account))
            {
                ModelState.AddModelError("Account", "登入帳號不可重複");
                return(View(systemuser));
            }

            if (ModelState.IsValid)
            {
                systemuser.ID = Guid.NewGuid();

                systemuser.Salt     = GenerateSalt();
                systemuser.Password = CryptographyPassword(systemuser.Password, systemuser.Salt);

                systemuser.CreateUser = WebSiteHelper.CurrentUserID;
                systemuser.CreateDate = DateTime.Now;
                systemuser.UpdateDate = DateTime.Now;

                db.SystemUser.Add(systemuser);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(systemuser));
        }
示例#2
0
        public ActionResult Create(Article article, HttpPostedFileBase[] uploads)
        {
            CheckFiles(uploads);

            if (ModelState.IsValid)
            {
                article.ID         = Guid.NewGuid();
                article.CreateUser = WebSiteHelper.CurrentUserID;
                article.CreateDate = DateTime.Now;
                article.UpdateDate = DateTime.Now;

                HandleFiles(article, uploads);

                db.Article.Add(article);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Category, "ID", "Name", article.CategoryID);
            return(View(article));
        }
        public ActionResult Create(Category category)
        {
            if (db.Category.Any(x => x.Name == category.Name))
            {
                ModelState.AddModelError("Name", "文章分類名稱不可重複");
                return(View(category));
            }

            if (ModelState.IsValid)
            {
                category.ID         = Guid.NewGuid();
                category.CreateUser = WebSiteHelper.CurrentUserID;
                category.CreateDate = DateTime.Now;
                category.UpdateDate = DateTime.Now;

                db.Category.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
 /// <summary>
 /// 增加文章瀏覽次數
 /// </summary>
 /// <param name="article">The article.</param>
 private void IncreaseViewCount(Article article)
 {
     article.ViewCount      += 1;
     db.Entry(article).State = EntityState.Modified;
     db.SaveChanges();
 }