protected async Task <ComponentsForDevice> GetMultipleComponents(int?deviceId, string typeName) { var selectListDevice = new SelectList(await _deviceService.GetAllAsync(), "Id", "Name"); var selectListTypes = new SelectList(await _componentBaseService.GetTypesAsync()); var components = new ComponentsForDevice(); List <TEntity> componentsInDevice = new List <TEntity>(); if (deviceId != null) { var device = selectListDevice.FirstOrDefault(l => l.Value == deviceId.ToString()); if (device != null) { device.Selected = true; } componentsInDevice = await _componentBaseService.GetByDeviceId(deviceId.Value); } else { componentsInDevice = await _componentBaseService.GetAllAsync(); } if (componentsInDevice == null) { throw new NotImplementedException(); } if (typeName != null) { var type = selectListTypes.FirstOrDefault(l => l.Text == typeName); if (type != null) { type.Selected = true; } componentsInDevice = componentsInDevice.Where(c => c.Type == typeName).ToList(); } var length = componentsInDevice.Count; components.ComponentId = new int[length]; components.ComponentName = new string[length]; components.Quantity = new int[length]; components.QuantityInStock = new int[length]; for (var index = 0; index < length; index++) { var componentInDevice = componentsInDevice[index]; components.ComponentName[index] = componentInDevice.ToString(); components.ComponentId[index] = componentInDevice.Id; components.QuantityInStock[index] = componentInDevice.Quantity; } ViewBag.TypeNames = selectListTypes; ViewBag.Devices = selectListDevice; return(components); }
public async Task <IActionResult> AddMultiple(int?deviceId, string typeName, ComponentsForDevice components) { if (components == null) { throw new Exception("Device not found."); } for (var index = 0; index < components.ComponentId.Length; index++) { await _componentBaseService.IncreaseQuantityAsync(components.ComponentId[index], components.Quantity[index]); } return(RedirectToAction(nameof(AddMultiple), new { deviceId, typeName })); }