public async Task <IActionResult> Create([Bind("ComponentTypeId,ComponentName,ComponentInfo,Location,Status,Datasheet,ImageUrl,Manufacturer,WikiLink,AdminComment")] ComponentType componentType, [Bind("Categories")] int[] categories)
        {
            if (ModelState.IsValid)
            {
                _context.Add(componentType);
                await _context.SaveChangesAsync();

                foreach (var categoryId in categories)
                {
                    var categoryFromdb        = _context.Categories.SingleOrDefault(x => x.CategoryId == categoryId);
                    var categoryComponentType = new CategoryComponentType
                    {
                        Category        = categoryFromdb,
                        CategoryId      = categoryId,
                        ComponentType   = componentType,
                        ComponentTypeId = componentType.ComponentTypeId
                    };

                    componentType.Categories.Add(categoryComponentType);
                    categoryFromdb.ComponentTypes.Add(categoryComponentType);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(componentType));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("ComponentTypeId,ComponentNumber,SerialNo,Status,AdminComment,UserComment,CurrentLoanInformationId")] Component component)
        {
            if (ModelState.IsValid)
            {
                _context.Add(component);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.ComponentTypes = await _context.ComponentTypes.ToListAsync();

            return(View(component));
        }