コード例 #1
0
        public void Post([FromBody] Author item)
        {
            BookStoreDataContext db = new BookStoreDataContext();

            db.Add(item);
            db.SaveChanges();
        }
コード例 #2
0
        public IActionResult Create(BookFormViewModel item)
        {
            ViewBag.Categories = new SelectList(db.Categories.ToList(), "CategoryID", "Name");
            ViewBag.Authors    = new MultiSelectList(db.Authors.ToList(), "AuthorID", "Name");

            if (ModelState.IsValid)
            {
                Book book = new Book();
                book.ISBN        = item.ISBN;
                book.CategoryID  = item.CategoryID;
                book.Title       = item.Title;
                book.PublishDate = item.PublishDate;
                book.Price       = item.Price;
                book.Quantity    = item.Quantity;
                db.Add(book);

                foreach (int authorId in item.AuthorIDs)
                {
                    BookAuthor bookAuthor = new BookAuthor();
                    bookAuthor.ISBN     = item.ISBN;
                    bookAuthor.AuthorID = authorId;
                    db.Add(bookAuthor);
                }

                db.SaveChanges();

                if (item.Photo != null)
                {
                    var file    = item.Photo;
                    var uploads = Path.Combine(_environment.WebRootPath, "upload");
                    if (file.Length > 0)
                    {
                        using (var fileStream = new FileStream(Path.Combine(uploads, item.ISBN + ".jpg"), FileMode.Create)){
                            file.CopyToAsync(fileStream);
                        }
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #3
0
        public IActionResult Create(Role item)
        {
            if (ModelState.IsValid)
            {
                db.Add(item);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #4
0
        public IActionResult Create(UserCreateFormViewModel item)
        {
            ViewBag.Roles = new SelectList(db.Roles.ToList(), "RoleID", "RoleName");

            if (ModelState.IsValid)
            {
                User user = new User();
                user.UserName = item.UserName;
                user.RoleID   = item.RoleID;
                user.Email    = item.Email;
                user.Password = EncryptString(item.Password, key);
                user.Fullname = item.Fullname;

                db.Add(user);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View());
        }