public ActionResult Edit(int id, HousingUpdate model) { HttpPostedFileBase file = Request.Files["ImageData"]; if (!ModelState.IsValid) { return(View(model)); } if (model.HousingId != id) { ModelState.AddModelError("", "ID Mismatch"); return(View(model)); } var service = CreateHousingService(); if (service.UpdateHousing(file, model)) { TempData["SaveResult"] = "Housing was updated"; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Housing could not be updated"); return(View(model)); }
// Housing Edit public ActionResult Edit(int id) { var service = CreateHousingService(); var detail = service.GetHousingDetail(id); var model = new HousingUpdate { HousingId = detail.HousingId, Name = detail.Name, Address = detail.Address, UnitsAvailable = detail.UnitsAvailable, AcceptVoucher = detail.AcceptVoucher, SectionType = detail.SectionType, Image = detail.Image }; return(View(model)); }
public bool UpdateHousing(HttpPostedFileBase file, HousingUpdate model) { model.Image = ConvertToBytes(file); using (var ctx = new ApplicationDbContext()) { var entity = ctx .Housings .Single(e => e.HousingId == model.HousingId); // && e.OwnerId == _userId entity.Name = model.Name; entity.Address = model.Address; entity.UnitsAvailable = model.UnitsAvailable; entity.AcceptVoucher = model.AcceptVoucher; entity.SectionType = model.SectionType; entity.Image = model.Image; return(ctx.SaveChanges() == 1); } }
// Update public int UpdateHousingById(HttpPostedFileBase file, int id, HousingUpdate newHousing) { newHousing.Image = ConvertToBytes(file); using (var ctx = new ApplicationDbContext()) { Housing housing = ctx.Housings.Find(id); if (housing == null) { return(2); } housing.Name = newHousing.Name; housing.Address = newHousing.Address; housing.UnitsAvailable = newHousing.UnitsAvailable; housing.AcceptVoucher = newHousing.AcceptVoucher; housing.SectionType = newHousing.SectionType; housing.Image = newHousing.Image; if (ctx.SaveChanges() == 1) { return(0); } return(1); } }