コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Room room, string[] selectedItems, string[] selectedMins, string[] selectedMaxs)
        {
            room.RoomItems = new List <ItemAssignment>();

            foreach (var item in selectedItems)
            {
                var key       = int.Parse(item) - 1;
                var minVal    = int.Parse(selectedMins[key]);
                var maxVal    = int.Parse(selectedMaxs[key]);
                var itemToAdd = new ItemAssignment
                {
                    RoomId = room.Id,
                    ItemId = int.Parse(item),
                    Min    = minVal,
                    Max    = maxVal
                };
                room.RoomItems.Add(itemToAdd);
            }

            if (ModelState.IsValid)
            {
                _context.Add(room);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(room));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,RoomId,Id,Min,Max")] ItemAssignment itemAssignment)
        {
            if (ModelState.IsValid)
            {
                _context.Add(itemAssignment);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["Id"]     = new SelectList(_context.Items, "Id", "Id", itemAssignment.ItemId);
            ViewData["RoomId"] = new SelectList(_context.Rooms, "RoomId", "RoomId", itemAssignment.RoomId);
            return(View(itemAssignment));
        }