// GET: Plant/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var plant = await _context.Plant .Include(p => p.PlantPlantType) .ThenInclude(q => q.PlantType) .SingleOrDefaultAsync(m => m.ID == id); if (plant == null) { return(NotFound()); } ViewData["HardinessZoneID"] = new SelectList(_context.HardinessZone, "ID", "Name", plant.HardinessZoneID); var viewmodel = new PlantEditViewModel(); viewmodel.Plant = plant; //var t = _context.PlantPlantType.Where(p => p.PlantID == id).ToList(); var selectedTags = _context.PlantPlantType.Where(p => p.PlantID == id).Select(q => q.PlantTypeID); var tags = _context.PlantType.Select(p => new TagViewModel() { PlantTypeID = p.ID, PlantType = p, IsSelected = selectedTags.Contains(p.ID) }).ToList(); viewmodel.Tags = tags; return(View(viewmodel)); }
public async Task <IActionResult> Edit(int id, [Bind("ID,HardinessZoneID,Name")] Plant plant, PlantEditViewModel plantVM) { if (id != plant.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(plant); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PlantExists(plant.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["HardinessZoneID"] = new SelectList(_context.HardinessZone, "ID", "Name", plant.HardinessZoneID); var viewmodel = new PlantEditViewModel(); viewmodel.Plant = plant; return(View(viewmodel)); }
public async Task <ActionResult> Edit([Bind(Include = "Key,Code,Name,Location")] PlantEditViewModel plant) { var pln = await db.Plants.Include("Organization").Where(x => x.Key == plant.Key).SingleOrDefaultAsync(); if (pln == null) { return(HttpNotFound()); } if (ModelState.IsValid) { pln.Name = plant.Name; pln.Code = plant.Code; pln.Location = plant.Location; db.Entry(pln).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(plant)); }