public ActionResult Edit(int id) { using (var ctx = new ApplicationDbContext()) { ViewBag.ShelterList = ctx.Shelters.Select (s => new SelectListItem() { Text = s.ShelterName, Value = s.ShelterId.ToString() } ).ToList(); } var service = new CatService(); var detail = service.GetCatById(id); var model = new CatEdit { CatId = detail.CatId, CatName = detail.CatName, CatSex = detail.CatSex, CatWeight = detail.CatWeight, CatAge = detail.CatAge, CatPrice = detail.CatPrice, CatImage = detail.CatImage, ShelterId = detail.ShelterId }; return(View(model)); }
public ActionResult Edit(int id) { var service = CreateCatService(); var detail = service.GetCatById(id); var model = new CatEdit { Name = detail.Name, ClientId = detail.ClientId }; return(View(model)); }
public bool UpdateCat(CatEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Cats .Single(e => e.CatId == model.CatId); entity.Name = model.Name; entity.ClientId = model.ClientId; return(ctx.SaveChanges() == 1); } }
//edit cat /// <summary> /// Allows user to edit information of cat /// </summary> /// <param name="cat"></param> /// <returns></returns> public IHttpActionResult Put(int id, [FromBody] CatEdit cat) { GetById(id); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateCatServices(); if (!service.UpdateCat(cat)) { return(InternalServerError()); } return(Ok("Cat has been updated! (◕‿◕✿)")); }
public bool UpdateCat(CatEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Cats .Single(e => e.CatId == model.CatId); entity.CatName = model.CatName; entity.CatSex = model.CatSex; entity.CatWeight = model.CatWeight; entity.CatAge = model.CatAge; entity.CatPrice = model.CatPrice; entity.ShelterId = model.ShelterId; entity.CatImage = model.CatImage; return(ctx.SaveChanges() == 1); } }
//update cat public bool UpdateCat(CatEdit model) { //add ownerId guid here? (shelter should be the only one able to delete/ update cats Cat oldCat = GetCatById(model.CatId); if (oldCat != null) { oldCat.IsDeclawed = model.IsDeclawed; oldCat.IsFat = model.IsFat; oldCat.Name = model.Name; oldCat.Breed = model.Breed; oldCat.Age = model.Age; oldCat.AboutMe = model.AboutMe; return(_context.SaveChanges() == 1); } else { return(false); } }
public ActionResult Edit(int id, CatEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.CatId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = CreateCatService(); if (service.UpdateCat(model)) { TempData["SaveResult"] = "Cat updated"; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Cat could not be updated."); return(View(model)); }