public IActionResult Upsert(ToyViewModel toyViewModel) { if (toyViewModel == null) { return(RedirectToAction(nameof(Index), "Toys")); } if (!ModelState.IsValid) { IEnumerable <Shop> shopList = _db.Shops.ToList(); toyViewModel = new ToyViewModel() { Toy = new Toy(), Shops = shopList.Select(I => new SelectListItem() { Text = I.Location, Value = I.Id.ToString() }) }; return(View(toyViewModel)); } if (toyViewModel.Toy.Id == 0) { _db.Toys.Add(toyViewModel.Toy); } else { _db.Toys.Update(toyViewModel.Toy); } _db.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public ActionResult Create(ToyViewModel toyVM)//[Bind(Include = "ToyID,ForAge,Price,TypeOfToyID,ImageUrl,Avaibility,YearOfManufactire")] { var toy = new Toy { NameOfToy = toyVM.Toy.NameOfToy, Price = toyVM.Toy.Price, ForAge = toyVM.Toy.ForAge, ToyDescription = toyVM.Toy.ToyDescription, Avaibility = toyVM.Toy.Avaibility, ImageUrl = toyVM.Toy.ImageUrl, YearOfManufactire = toyVM.Toy.YearOfManufactire, //ToyID=toyVM.Toy.ToyID, TypeOfToyID = toyVM.Toy.TypeOfToyID, TypeOfToy = toyVM.Toy.TypeOfToy }; if (ModelState.IsValid) { db.Toys.Add(toy); db.SaveChanges(); return(RedirectToAction("Index")); } //var model = new ToyViewModel //{ // Toy = toy, // TypeOfToys = db.ToysType.ToList() //}; // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID); toyVM.TypeOfToys = db.ToysType.ToList(); return(View(toyVM)); }
[ValidateInput(false)] //if there is description public ActionResult Edit(ToyViewModel toyVM) //[Bind(Include = "ToyID,ForAge,Price,TypeOfToyID,ImageUrl,Avaibility,YearOfManufactire")] { var toy = new Toy { ToyID = toyVM.Toy.ToyID, NameOfToy = toyVM.Toy.NameOfToy, Price = toyVM.Toy.Price, ToyDescription = toyVM.Toy.ToyDescription, ForAge = toyVM.Toy.ForAge, Avaibility = toyVM.Toy.Avaibility, ImageUrl = toyVM.Toy.ImageUrl, YearOfManufactire = toyVM.Toy.YearOfManufactire, TypeOfToyID = toyVM.Toy.TypeOfToyID, TypeOfToy = toyVM.Toy.TypeOfToy }; if (ModelState.IsValid) { db.Entry(toy).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } toyVM.TypeOfToys = db.ToysType.ToList(); // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID); return(View(toy));// return View(toyVM); }
public async Task <IActionResult> PutToy(int id, ToyViewModel toyModel) { if (id != toyModel.Id) { return(BadRequest()); } var toy = new Toy { Id = id, Name = toyModel.Name, Price = Currency.Parse(toyModel.Price) }; _context.Entry(toy).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ToyExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }
public IActionResult Upsert(int?id) { IEnumerable <Shop> shopList = _db.Shops.ToList(); ToyViewModel toyViewModel = new ToyViewModel() { Toy = new Toy(), Shops = shopList.Select(I => new SelectListItem { Text = I.Location, Value = I.Id.ToString() }) }; if (id == null) { return(View(toyViewModel)); } toyViewModel.Toy = _db.Toys.Find(id); if (toyViewModel.Toy == null) { return(NotFound()); } return(View(toyViewModel)); }
public IActionResult Create(ToyViewModel model) { if (ModelState.IsValid) { model.Toy.Producer = _producerRepository.GetProducerByID(model.Producer); model.Toy.Category = _categoryRepository.GetCategoryById(model.Category); _toyRepository.AddToy(model.Toy); if (model.Images != null) { var helper = new ImageHelper(_environment); foreach (var image in model.Images) { var imageName = helper.AddImage(image); var gallery = new Gallery() { FileName = imageName, Toy = model.Toy }; _galleryRepository.AddImage(gallery); } } _toyRepository.Save(); return(RedirectToAction("Index")); } else { model.Categories = FormHelper.GetFormCategories(_context.Categories.ToArray()); model.Producers = FormHelper.GetFormProducers(_context.Producers.ToArray()); } return(View(model)); }
public ToyView() { InitializeComponent(); _toyViewModel = new ToyViewModel(); DataContext = _toyViewModel; CbActive.IsChecked = true; ImageDefault(_imageDefault); }
// GET: Toys/Create public ActionResult Create() { // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name"); var typeoftoy = db.ToysType.ToList(); var model = new ToyViewModel { TypeOfToys = typeoftoy }; return(View(model)); }
public async Task AddNewToy(ToyViewModel model) { try { await _context.Toys.AddAsync(_mapper.Map <Toy>(model)); await _context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> PostLendTableEntry(ToyViewModel toyModel) { var toy = new Toy { Name = toyModel.Name, Price = Currency.Parse(toyModel.Price) }; _context.Toy.Add(toy); await _context.SaveChangesAsync(); return(Ok()); }
// GET: Item/Create public IActionResult Create() { var producers = FormHelper.GetFormProducers(_context.Producers.ToArray()); var categories = FormHelper.GetFormCategories(_context.Categories.ToArray()); var vm = new ToyViewModel() { Producers = producers, Categories = categories, Conditions = FormHelper.GetFormCondition() }; return(View(vm)); }
public async Task <ActionResult <ToyViewModel> > GetToy(int id) { var toy = await _context.Toy.FindAsync(id); if (toy == null) { return(NotFound()); } var result = new ToyViewModel { Id = toy.Id, Name = toy.Name, Price = Currency.Format(toy.Price) }; return(Ok(result)); }
public async Task <IActionResult> AddToy([FromBody] ToyViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { await _toyService.AddNewToy(model); return(Ok("New toy was added!")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
// GET: Toys/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Toy toy = db.Toys.Find(id); if (toy == null) { return(HttpNotFound()); } var model = new ToyViewModel { Toy = toy, TypeOfToys = db.ToysType.ToList() }; return(View(model)); }
// GET: Toys/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Toy toy = db.Toys.Find(id); if (toy == null) { return(HttpNotFound()); } var model = new ToyViewModel { Toy = toy, TypeOfToys = db.ToysType.ToList() }; // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID); return(View(model)); }
// GET: Item/Edit/5 public IActionResult Edit(int?id) { if (id == null) { return(NotFound()); } var _toy = _toyRepository.GetToyById(id.Value); var model = new ToyViewModel() { Toy = _toy, Producers = FormHelper.GetFormProducers(_producerRepository.GetProducers()), Categories = FormHelper.GetFormCategories(_categoryRepository.GetCategories()), Producer = _toy.Producer.Id, Category = _toy.Category.Id, Conditions = FormHelper.GetFormCondition(), Condition = (int)_toy.Condition }; return(View(model)); }
public IActionResult Edit(int id, ToyViewModel model) { if (id != model.Toy.ToyID) { return(NotFound()); } if (ModelState.IsValid) { model.Toy.Producer = _producerRepository.GetProducerByID(model.Producer); model.Toy.Category = _categoryRepository.GetCategoryById(model.Category); model.Toy.Condition = (Condition)model.Condition; _context.Update(model.Toy); if (model.Images != null) { var helper = new ImageHelper(_environment); foreach (var image in model.Images) { var imageName = helper.AddImage(image); var gallery = new Gallery() { FileName = imageName, Toy = model.Toy }; _galleryRepository.AddImage(gallery); } } _toyRepository.Save(); return(RedirectToAction("Index")); } return(View(model)); }