public IActionResult CreateItem() { if (HttpContext.Session.GetString("Username") != "Admin") { return(RedirectToAction("Index", "Home")); } ItemCreationViewModel icvm = new ItemCreationViewModel(); return(View(icvm)); }
public IActionResult Save(ItemCreationViewModel icvm) { if (ModelState.IsValid) { if (icvm.ImageFile.Length <= 0 || !icvm.ImageFile.ContentType.Contains("image")) { ModelState.AddModelError("ImageFile", "Selected file is not an image."); return(View("CreateItem", icvm)); } Item tempitem = ItemToItemVM.ToItem(icvm); _ir.Create(tempitem); return(RedirectToAction("AllItems", "Item")); } return(View("CreateItem", icvm)); }
static public Item ToItem(ItemCreationViewModel icvm) { MemoryStream memoryStream = new MemoryStream(); icvm.ImageFile.CopyTo(memoryStream); var item = new Item() { Id = icvm.Id, Name = icvm.Name, ItemType = (Library.Models.ItemType)icvm.ItemType, ImageFile = memoryStream.ToArray() }; return(item); }