コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,PostTitle,PostBody,PostDate,PostedBy")] Comment comment, int itemId)
        {
            if (ModelState.IsValid)
            {
                comment.PostDate = DateTime.Now;
                comment.Item     = await _context.Item.FirstOrDefaultAsync(i => (i.Id == itemId));

                comment.PostedBy = HttpContext.Session.GetString("FullName");

                var item = await _context.Item
                           .Include(x => x.Comments)
                           .FirstOrDefaultAsync(x => x.Id == itemId);

                item.Comments.Add(comment);

                _context.Item.Update(item);
                _context.Add(comment);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(comment));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,Type,Name,Price")] Item item)
        {
            if (ModelState.IsValid)
            {
                _context.Add(item);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(item));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("BranchId,ItemId")] BranchItem branchItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(branchItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["BranchId"] = new SelectList(_context.Set <Branch>(), "Id", "Id", branchItem.BranchId);
            ViewData["ItemId"]   = new SelectList(_context.Item, "Id", "Id", branchItem.ItemId);
            return(View(branchItem));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("Id,Type,FirstName,LastName,Email,Password,Birthday")] User user, int itemId)
        {
            if (ModelState.IsValid)
            {
                user.FavoriteItem = await _context.Item.FirstOrDefaultAsync(i => (i.Id == itemId));

                var item = await _context.Item
                           .Include(x => x.SatisfiedUsers)
                           .FirstOrDefaultAsync(x => x.Id == itemId);

                item.SatisfiedUsers.Add(user);

                _context.Item.Update(item);
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(user));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("Id,Address,City,District")] Branch branch, int[] ItemId)
        {
            if (ModelState.IsValid)
            {
                branch.BranchItems = new List <BranchItem>();

                foreach (var id in ItemId)
                {
                    branch.BranchItems.Add(new BranchItem()
                    {
                        BranchId = branch.Id, ItemId = id
                    });
                }

                _context.Add(branch);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(branch));
        }