示例#1
0
        public async Task <IActionResult> Edit(CategoryVM categoryVM)
        {
            if (ModelState.IsValid)
            {
                //Map model to viewmodels
                var category = await db.Categories.FindAsync(categoryVM.Id);

                category.Name     = categoryVM.Name;
                category.ParentId = categoryVM.ParentId;

                try
                {
                    db.Update(category);
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to save changes. " +
                                                 "Try again, and if the problem persists, " +
                                                 "see your system administrator.");
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ParentId"] = new SelectList(db.Categories, "Id", "Id", categoryVM.ParentId);
            return(View(categoryVM));
        }
        public async Task <IActionResult> Edit(int id, [Bind("AuthorId,FirstName,LastName,DateOfBirth")] Author author)
        {
            if (id != author.AuthorId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(author);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(author.AuthorId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
示例#3
0
        public async Task <IActionResult> Edit(byte id, [Bind("CategoryId,Name")] Category category)
        {
            if (id != category.CategoryId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.CategoryId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Edit(AccountVM accountVM)
        {
            var account = await db.Accounts.FindAsync(accountVM.Id);

            if (accountVM.Photo != null)
            {
                string fileName = await UploadFile.UploadAsync(webHostEnvironment, accountVM.Photo, photoPath);

                account.Photo = fileName;
            }

            layoutVM.AccountVM = AccountUtility.MapModelToVM(account);

            try
            {
                db.Update(account);
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccountExists(account.Id))
                {
                    return(NotFound());
                }
                else
                {
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }


                return(RedirectToAction(nameof(Login)));
            }

            return(View(layoutVM));
        }
示例#5
0
        public async Task <IActionResult> Edit(ItemVM itemVM)
        {
            var item = await db.Items.FindAsync(itemVM.Id);

            if (itemVM.Photo != null)
            {
                string fileName = await UploadFile.UploadAsync(webHostEnvironment, itemVM.Photo, photoPath);

                item.Photo = fileName;
            }

            if (itemVM.Document != null)
            {
                string documentName = await UploadFile.UploadAsync(webHostEnvironment, itemVM.Document, documentPath);

                item.Document = documentName;
            }

            item.Title        = itemVM.Title;
            item.Description  = itemVM.Description;
            item.MinimumBid   = itemVM.MinimumBid;
            item.CreatedAt    = itemVM.CreatedAt;
            item.BidStatus    = itemVM.BidStatus;
            item.BidEndDate   = itemVM.BidEndDate;
            item.BidStartDate = itemVM.BidStartDate;
            if (itemVM.SelectedCategoryIds != null)
            {
                var categoryitems = db.CategoryItems.Where(c => c.ItemId == itemVM.Id).ToList();
                if (categoryitems.Count > 0)
                {
                    foreach (var categoryitem in categoryitems)
                    {
                        item.CategoryItems.Remove(categoryitem);
                    }
                }
                foreach (var cateId in itemVM.SelectedCategoryIds)
                {
                    item.CategoryItems.Add(new CategoryItem
                    {
                        CategoryId = cateId,
                        ItemId     = item.Id,
                        CreatedAt  = DateTime.Now
                    });
                }
                db.Items.Add(item);

                try
                {
                    db.Update(item);
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ItemExists(item.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        ModelState.AddModelError("", "Unable to save changes. " +
                                                 "Try again, and if the problem persists, " +
                                                 "see your system administrator.");
                    }
                }

                return(RedirectToAction("Detail", "Item", new { id = itemVM.Id }));
            }

            layoutVM.ItemVM            = itemVM;
            layoutVM.ItemVM.Categories = db.Categories.Select(a =>
                                                              new SelectListItem
            {
                Value = a.Id.ToString(),
                Text  = a.Name
            }).ToList();

            return(View(layoutVM));
        }