public async Task <IActionResult> Create(Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ComponentCategoriesIndex))); } return(View(category)); }
public async Task <IActionResult> Create(ComponentTypeViewModel componentType) { if (ModelState.IsValid) { var componentTypeModel = new ComponentType() { ComponentName = componentType.ComponentName, ComponentInfo = componentType.ComponentInfo, Status = componentType.Status, Datasheet = componentType.Datasheet, WikiLink = componentType.WikiLink, Manufacturer = componentType.Manufacturer, AdminComment = componentType.AdminComment, ImageUrl = componentType.ImageUrl, Location = componentType.Location, CategorieIdsList = componentType.SelectedCategories }; if (!componentType.Image.FileName.EndsWith(".jpg") && !componentType.Image.FileName.EndsWith(".png")) { ViewBag.ImgError = "Please Insert an image of the right file type"; return(View("Edit", componentType)); } if (componentType.Image != null) { using (var ms = new MemoryStream()) { await componentType.Image.CopyToAsync(ms); var fileBytes = ms.ToArray(); componentTypeModel.Image = fileBytes; componentTypeModel.ImageMimeType = componentType.Image.ContentType; componentTypeModel.FileName = componentType.Image.FileName; } } _context.Add(componentTypeModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ComponentTypesIndex))); } return(View(componentType)); }