public async Task <IActionResult> Create([Bind("CategoryId,CategoryName")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(ViewPath + "Create.cshtml", category)); }
public async Task <IActionResult> Create([Bind("SupplierId,SupplierName,PhoneNumberHome,PhoneNumberWork,PhoneNumberMobile,Email")] Supplier supplier) { if (ModelState.IsValid) { _context.Add(supplier); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(ViewPath + "Create.cshtml", supplier)); }
public async Task <IActionResult> Create([Bind("BookId,BookName,ShortDescription,Description,Price,Author,PreferredFlag,CategoryId,SupplierId")] Book book, IList <IFormFile> _files) { var relativeName = ""; var fileName = ""; if (_files == null || _files.Count != 1) { relativeName = "bookdefault.jpg"; } else { fileName = ContentDispositionHeaderValue .Parse(_files[0].ContentDisposition) .FileName .Trim('"'); //Path for localhost relativeName = DateTime.Now.ToString("ddMMyyyy-HHmmssffffff") + fileName; using (FileStream fs = System.IO.File.Create(_hostingEnv.WebRootPath + "/images/" + relativeName)) { await _files[0].CopyToAsync(fs); fs.Flush(); } } book.ImageUrl = relativeName; try { if (ModelState.IsValid) { _context.Add(book); await _context.SaveChangesAsync(); //save the image uploaed into wwwroot/images folder return(RedirectToAction(nameof(Index))); } ViewData["Category"] = new SelectList(_context.Categories, "CategoryId", "CategoryName", book.CategoryId); ViewData["Supplier"] = new SelectList(_context.Suppliers, "SupplierId", "SupplierName", book.SupplierId); return(View(ViewPath + "Create.cshtml", book)); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(book)); }