示例#1
0
        public async Task <IActionResult> Create(CategoryVM categoryVM)
        {
            ViewBag.CategoryAdd  = "Failed";
            categoryVM.CreatedAt = DateTime.Now;
            if (categoryVM != null)
            {
                var category = CategoryUtility.MapVMtoModel(categoryVM);

                //stop admin add 3rd category
                if (category.ParentId == null)
                {
                    category.Level = 1;
                }
                else if (CategoryExists(category.ParentId) == true)
                {
                    category.Level = 2;
                }

                db.Add(category);
                await db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(categoryVM));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("AuthorId,FirstName,LastName,DateOfBirth")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
示例#4
0
        public async Task <IActionResult> Create(CreateArtwork artWork)
        {
            if (ModelState.IsValid)
            {
                string type = artWork.Image.ContentType.Split("/")[0];
                if (type != "image")
                {
                    throw new InvalidDataException();
                }
                string currentFileName = artWork.Image.FileName.Trim('"');
                string fileExtension   = Path.GetExtension(currentFileName);
                string newFileName     = Guid.NewGuid().ToString() + fileExtension;
                string semiPath        = $@"images\artworkimages\{newFileName}";
                string filePath        = Path.Combine(hostingEnvironment.WebRootPath, semiPath);
                string dbPath          = $"/images/artworkimages/{newFileName}";
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    artWork.Image.CopyTo(stream);
                    stream.Flush();
                }
                var newArtwork = new ArtWork
                {
                    Name       = artWork.Name,
                    CategoryId = artWork.CategoryId,
                    AuthorId   = artWork.AuthorId,
                    Caption    = artWork.Caption,
                    Sold       = false,
                    Image      = dbPath
                };
                _context.Add(newArtwork);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Author"] = _context.Authors.Select(a => new SelectListItem
            {
                Text  = $"{a.FirstName} {a.LastName}",
                Value = a.AuthorId.ToString()
            });
            ViewData["Category"] = new SelectList(_context.Categories, "CategoryId", "Name");
            return(View(artWork));
        }