// GET: ComponentTypes/Create public IActionResult Create() { var comTypeVM = new ComTypeViewModel() { Categories = _context.Category.ToList().Select(category => new SelectListItem() { Text = category.Name, Value = category.CategoryId.ToString() }).ToList() }; return(View(comTypeVM)); }
// GET: ComponentTypes/Edit/5 public async Task <IActionResult> Edit(long?id) { if (id == null) { return(NotFound()); } var componentType = await _context.ComponentType.SingleOrDefaultAsync(x => x.ComponentTypeId == id); if (componentType == null) { return(NotFound()); } var categoryIds = await _context.ComponentCategoryTypes .Where(com => com.ComponentTypeId == componentType.ComponentTypeId).Select(i => i.CategoryId).ToListAsync(); var categoryNames = new List <string>(); foreach (var categoryId in categoryIds) { var categoryName = await _context.Category.Where(cat => cat.CategoryId == categoryId).Select(i => i.Name) .FirstOrDefaultAsync(); if (categoryName != null) { categoryNames.Add(categoryName); } } var comTypeVM = new ComTypeViewModel() { ComponentType = componentType, ChoosenCategories = categoryNames, Categories = _context.Category.ToList().Select(x => new SelectListItem() { Text = x.Name, Value = x.CategoryId.ToString() }).ToList() }; return(View(comTypeVM)); }
// GET: ComponentTypes/Details/5 public async Task <IActionResult> Details(long?id) { if (id == null) { return(NotFound()); } var componentType = await _context.ComponentType.Include(x => x.Image) .FirstOrDefaultAsync(m => m.ComponentTypeId == id); if (componentType == null) { return(NotFound()); } var categoryIds = await _context.ComponentCategoryTypes .Where(com => com.ComponentTypeId == componentType.ComponentTypeId).Select(i => i.CategoryId).ToListAsync(); var categoryNames = new List <string>(); foreach (var categoryId in categoryIds) { var categoryName = await _context.Category.Where(cat => cat.CategoryId == categoryId).Select(i => i.Name) .FirstOrDefaultAsync(); if (categoryName != null) { categoryNames.Add(categoryName); } } var comTypeVM = new ComTypeViewModel() { ComponentType = componentType, ChoosenCategories = categoryNames }; return(View(comTypeVM)); }
public async Task <IActionResult> Edit(ComTypeViewModel componentTypeViewModel) { if (!ModelState.IsValid) { return(RedirectToAction(nameof(Edit), componentTypeViewModel.ComponentType.ComponentTypeId)); } try { _context.ComponentCategoryTypes.RemoveRange(_context.ComponentCategoryTypes.Where(x => x.ComponentTypeId == componentTypeViewModel.ComponentType.ComponentTypeId)); if (componentTypeViewModel.ChoosenCategories != null) { var choosenCategories = _context.Category.Where(x => componentTypeViewModel.ChoosenCategories.Contains(x.CategoryId.ToString())); var componentCategoryTypes = new List <ComponentCategoryType>(); foreach (var choosenCategory in choosenCategories) { componentCategoryTypes.Add(new ComponentCategoryType() { CategoryId = choosenCategory.CategoryId, ComponentType = componentTypeViewModel.ComponentType }); } componentTypeViewModel.ComponentType.ComponentCategoryTypes = componentCategoryTypes; } var image = new ESImage(); Bitmap target = new Bitmap(64, 64); Bitmap imageFullSize; if (componentTypeViewModel.Image != null) { var filename = Path.GetExtension(componentTypeViewModel.Image.FileName.ToLower()); if (!filename.Contains(".jpg") && !filename.Contains(".jpeg") && !filename.Contains(".png") && !filename.Contains(".bmp") && !filename.Contains(".gif")) { return(RedirectToAction(nameof(Edit), componentTypeViewModel.ComponentType.ComponentTypeId)); } using (var memoryStream = new MemoryStream()) { await componentTypeViewModel.Image.CopyToAsync(memoryStream); image.ImageData = memoryStream.ToArray(); imageFullSize = new Bitmap(memoryStream); } using (var graphics = Graphics.FromImage(target)) { graphics.CompositingQuality = CompositingQuality.HighSpeed; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingMode = CompositingMode.SourceCopy; graphics.DrawImage(imageFullSize, 0, 0, 64, 64); using (var memoryStream = new MemoryStream()) { target.Save(memoryStream, getImageFormat(filename)); image.Thumbnail = memoryStream.ToArray(); } } image.ImageMimeType = "image/" + filename.Remove(0, 1); componentTypeViewModel.ComponentType.Image = image; _context.EsImages.Add(image); } _context.Update(componentTypeViewModel.ComponentType); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ComponentTypeExists(componentTypeViewModel.ComponentType.ComponentTypeId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); }