public ActionResult EditPet(PetPicViewModel model) { DAL.Pet pet = new DAL.Pet(); if (ModelState.IsValid) { var identityID = User.Identity.GetUserId(); var loggedID = db.PetopiaUsers.Where(x => x.ASPNetIdentityID == identityID) .Select(x => x.UserID).First(); int ownerID = db.PetOwners.Where(x => x.UserID == loggedID) .Select(x => x.PetOwnerID).First(); pet.PetOwnerID = ownerID; pet.PetName = model.PetName; pet.Species = model.Species; pet.Breed = model.Breed; pet.Gender = model.Gender; pet.Birthdate = model.Birthdate; pet.PetCaption = model.PetCaption; pet.PetBio = model.PetBio; pet.Weight = model.Weight; pet.HealthConcerns = model.HealthConcerns; pet.BehaviorConcerns = model.BehaviorConcerns; pet.PetAccess = model.PetAccess; pet.EmergencyContactName = model.EmergencyContactName; pet.EmergencyContactPhone = model.EmergencyContactPhone; pet.NeedsDetails = model.NeedsDetails; pet.PetID = model.PetID; // pet profile picture if (model.PetPhoto != null) { if (model.PetPhoto.ContentLength > (4 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "Image can not be lager than 4MB."); return(View(model)); } if (!(model.PetPhoto.ContentType == "image/jpeg")) { ModelState.AddModelError("CustomError", "Image must be in jpeg format."); return(View(model)); } byte[] data = new byte[model.PetPhoto.ContentLength]; model.PetPhoto.InputStream.Read(data, 0, model.PetPhoto.ContentLength); pet.PetPhoto = data; } else //If no pic was uploaded, we need to seed the current profile pic into our user { pet.PetPhoto = db.Pets.Where(x => x.PetID == pet.PetID).Select(x => x.PetPhoto).FirstOrDefault(); } //----------------------------------------------------- db.Entry(pet).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("PetProfile", new { id = pet.PetID })); } // pick list for rating -- like 1 thru 5 ViewBag.PetOwnerID = new SelectList(db.PetOwners, "PetOwnerID", "AverageRating", pet.PetOwnerID); // boy-girl-altered pick-list ViewBag.GenderList = genderSelectList; return(View(model)); }
//=============================================================================== // EDIT PET -- GET //=============================================================================== // GET: Pets/EditPet/5 public ActionResult EditPet(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DAL.Pet pet = db.Pets.Find(id); if (pet == null) { return(HttpNotFound()); } //--------------------------------------------------------- PetPicViewModel model = new PetPicViewModel(); model.PetName = pet.PetName; model.Species = pet.Species; model.Breed = pet.Breed; model.Gender = pet.Gender; model.Birthdate = pet.Birthdate; model.Weight = pet.Weight; model.HealthConcerns = pet.HealthConcerns; model.BehaviorConcerns = pet.BehaviorConcerns; model.PetAccess = pet.PetAccess; model.EmergencyContactName = pet.EmergencyContactName; model.EmergencyContactPhone = pet.EmergencyContactPhone; model.NeedsDetails = pet.NeedsDetails; model.PetCaption = pet.PetCaption; model.PetBio = pet.PetBio; model.PetID = pet.PetID; // pick-list for rating -- 1 thru 5 ViewBag.PetOwnerID = new SelectList(db.PetOwners, "PetOwnerID", "AverageRating", pet.PetOwnerID); // boy-girl-altered pick-list ViewBag.GenderList = genderSelectList; //--------------------------------------------------------------------------- // testing to find this Pet's owner -- // to ONLY show details/appts/editPet buttons to the Pet's owner! // find this Pet's Owner's ID var thisPetsOwnersID = db.Pets.Where(p => p.PetID == id) .Select(poID => poID.PetOwnerID).FirstOrDefault(); // now pull this Pet Owner's PetopiaUser ID var thisPetsOwnersPetopiaUserID = db.PetOwners.Where(pu => pu.PetOwnerID == thisPetsOwnersID) .Select(puID => puID.UserID).FirstOrDefault(); // now pull this PetopiaUser's ASPNetIdentityID var thisPetsOwnersASPNetIdentityID = db.PetopiaUsers.Where(pu => pu.UserID == thisPetsOwnersPetopiaUserID) .Select(aspnetID => aspnetID.ASPNetIdentityID) .FirstOrDefault(); // now pull the logged-in user's ID var loggedInUser = User.Identity.GetUserId(); // so kinda backwards from the queries in the profile or care transaction controllers! ViewBag.thisPetsOwnersASPNetIdentityID = thisPetsOwnersASPNetIdentityID; ViewBag.loggedInUser = loggedInUser; //--------------------------------------------------------------------------- return(View(model)); }