public ActionResult Create() { var model = new App.Web.Areas.Admin.Models.GoodViewModel { Good = new ItemForSale(), Categories = new MultiSelectList(this.UnitOfWork.CategoryRepository.Get(), "Id", "Title"), Users = new MultiSelectList(this.UnitOfWork.UserRepository.Get(), "Id", "NickName") }; return View(model); }
public ActionResult Create(App.Web.Areas.Admin.Models.GoodViewModel viewModel) { try { if (!ModelState.IsValid) throw new Exception(); var good = viewModel.Good; good.SellerId = viewModel.SelectedUserId.Value; var ids = viewModel.SelectedCategoryIds; if (ids != null && ids.Length > 0) { var categories = new List<Category>(); foreach (var id in ids) { var category = this.UnitOfWork.CategoryRepository.GetByID(id); categories.Add(category); } good.Categories = categories; } this.UnitOfWork.ItemForSaleRepository.Insert(good); this.UnitOfWork.Save(); return RedirectToAction("Index", "Good", new { area = "Admin" }); } catch { var model = new App.Web.Areas.Admin.Models.GoodViewModel { Good = viewModel.Good, Categories = new MultiSelectList(this.UnitOfWork.CategoryRepository.Get(), "Id", "Title"), Users = new MultiSelectList(this.UnitOfWork.UserRepository.Get(), "Id", "NickName") }; return View(model); } }
public ActionResult Edit(Int64? id) { try { var good = this.UnitOfWork.ItemForSaleRepository.GetByID(id); if (good == null) throw new Exception(); Int64? user_id = null; user_id = good.SellerId; int[] ids = null; if (good.Categories != null && good.Categories.Count > 0) { ids = new int[good.Categories.Count]; int i = 0; foreach (var category in good.Categories) { ids[i] = category.Id; i++; } } var model = new App.Web.Areas.Admin.Models.GoodViewModel { Good = this.UnitOfWork.ItemForSaleRepository.GetByID(id), Categories = new MultiSelectList(this.UnitOfWork.CategoryRepository.Get(), "Id", "Title"), SelectedCategoryIds = ids, Users = new MultiSelectList(this.UnitOfWork.UserRepository.Get(), "Id", "NickName"), SelectedUserId = user_id }; return View(model); } catch { return RedirectToAction("Index", "Good", new { area = "Admin" }); } }
public ActionResult Edit(App.Web.Areas.Admin.Models.GoodViewModel viewModel) { try { if (!ModelState.IsValid) throw new Exception(); var originalGood = this.UnitOfWork.ItemForSaleRepository.GetByID(viewModel.Good.Id); if (originalGood == null) throw new Exception(); Int64 NewSellerId = viewModel.Good.SellerId; if (NewSellerId != viewModel.SelectedUserId){ NewSellerId = viewModel.SelectedUserId.Value; } originalGood.Title = viewModel.Good.Title; originalGood.Description = viewModel.Good.Description; originalGood.Price = viewModel.Good.Price; originalGood.SellerId = NewSellerId; originalGood.Deadline = viewModel.Good.Deadline; originalGood.ModifiedDate = DateTime.Now; var ids = viewModel.SelectedCategoryIds; if (ids != null && ids.Length > 0) { if (originalGood.Categories != null) originalGood.Categories.Clear(); var categories = new List<Category>(); foreach (var id in ids) { var category = this.UnitOfWork.CategoryRepository.GetByID(id); categories.Add(category); } originalGood.Categories = categories; } this.UnitOfWork.ItemForSaleRepository.Update(originalGood); this.UnitOfWork.Save(); return RedirectToAction("Index", "Good", new { area = "Admin" }); } catch { var model = new App.Web.Areas.Admin.Models.GoodViewModel { Good = viewModel.Good, SelectedCategoryIds = viewModel.SelectedCategoryIds, Categories = new MultiSelectList(this.UnitOfWork.CategoryRepository.Get(), "Id", "Title"), Users = new MultiSelectList(this.UnitOfWork.UserRepository.Get(), "Id", "NickName"), SelectedUserId = null }; return View(model); } }