public ActionResult Create(CreatDogVM model) { try { if (ModelState.IsValid) { var file = model.Photo; if (file != null) { Dog dog = new Dog { Name = model.Name, Age = model.Age, Weight = model.Weight, Gender = model.Gender, Breed = model.Breed, AddDate = DateTime.Now }; dogService.Insert(dog); int id = dog.Id; dog.PhotoPath = "/Images/Dogs/" + id + ".jpg"; file.SaveAs(Server.MapPath("/Images/Dogs/" + id + ".jpg")); dogService.Update(dog); ViewBag.Message = "Dog data added successfully."; } else { ViewBag.Error = "Image not found!"; } } } catch (Exception e) { ViewBag.Exception = e.InnerException.InnerException.Message; ViewBag.Error = "Something went wrong!"; } return(View(model)); }
public ActionResult Edit(int?id, CreatDogVM model) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } try { if (ModelState.IsValid) { var file = model.Photo; if (file != null) { file.SaveAs(Server.MapPath("/Images/Dogs/" + id + ".jpg")); } Dog dog = new Dog { Id = (int)id, Name = model.Name, Breed = model.Breed, Age = model.Age, Weight = model.Weight, Gender = model.Gender, AddDate = model.AddDate, PhotoPath = model.Path }; dogService.Update(dog); ViewBag.Message = "Dog data updated successfully."; } return(View(model)); } catch (Exception e) { ViewBag.Exception = e.InnerException.InnerException.Message; ViewBag.Error = "Something went wrong!"; } return(View(model)); }
public ActionResult Edit(int?id) { /*if (Session["AdminEmail"] == null) * { * return RedirectToAction("Login", "Admin"); * }*/ if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Dog dog = dogService.GetById(id); CreatDogVM model = new CreatDogVM { Name = dog.Name, Age = dog.Age, Weight = dog.Weight, Gender = dog.Gender, Breed = dog.Breed, Path = dog.PhotoPath, AddDate = dog.AddDate }; return(View(model)); }