public async Task <IActionResult> Edit(int CategoryId, string Name) { if (ModelState.IsValid) { try { var temp = await _context.Category.FirstOrDefaultAsync(x => x.CategoryId == CategoryId); temp.Name = Name; _context.Update(temp); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(ComponentCategoriesIndex))); } return(View()); }
public async Task <IActionResult> Edit([FromBody] 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, }; if (!componentType.Image.FileName.EndsWith(".jpg")) { ViewBag.ImgError = "Please Insert an image of the right file type"; return(View("Edit", componentType)); } if (componentType.Image != null) { using (var reader = new StreamReader(componentType.Image.OpenReadStream())) { string contentAsString = reader.ReadToEnd(); byte[] contentAsByteArray = GetBytes(contentAsString); componentTypeModel.Image = contentAsByteArray; componentTypeModel.ImageMimeType = componentType.Image.ContentType; componentTypeModel.FileName = componentType.Image.FileName; } } _context.Update(componentTypeModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ComponentTypesIndex))); } return(View(componentType)); }