public ActionResult CreatePackage(PackageViewModel packageDetails) { try { IPackageTypeBL packageType = new PackageTypeBL(); ViewBag.PackageTypes = new SelectList(packageType.GetPackageTypes(), "PackageTypeId", "PackageTypeName"); if (ModelState.IsValid) { string fileName = Path.GetFileNameWithoutExtension(packageDetails.ImageFile.FileName); string extension = Path.GetExtension(packageDetails.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; packageDetails.ImageSource = "~/Images/PackageImages/" + fileName; Package package = AutoMapper.Mapper.Map <PackageViewModel, Package>(packageDetails); fileName = Path.Combine(Server.MapPath("~/Images/PackageImages/"), fileName); packageDetails.ImageFile.SaveAs(fileName); packages.AddPackage(package); TempData["Message"] = "Package Added"; return(RedirectToAction("ViewPackage")); } return(View()); } catch { return(RedirectToAction("Error", "Error")); } }
public ActionResult AddPackage(PackageModel newPackage) { if (ModelState.IsValid) { bool CanAddPackage = packageDetails.VerifyPackage(newPackage.PackageName); if (CanAddPackage) { var package = AutoMapper.Mapper.Map <PackageModel, Package>(newPackage); //Automapping package details from model to entity packageDetails.AddPackage(package); //Adding the package to the database return(RedirectToAction("DisplayPackages")); //Redirecting after adding the package } TempData["Message"] = "Package already exists!!"; } return(View()); }