public async Task <IActionResult> Edit(ContractorEditViewModels contractor) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadFile(contractor); Contractor updateContractor = await db.Contractors.FindAsync(contractor.Id); updateContractor.Name = contractor.Name; updateContractor.Email = contractor.Email; updateContractor.PrintEmail = contractor.PrintEmail; updateContractor.Contact = contractor.Contact; updateContractor.Address = contractor.Address; updateContractor.CityId = contractor.CityId; if (contractor.Image != null) { if (contractor.ExistingPhotoPath != null) { string filePath = Path.Combine(env.WebRootPath + "images" + contractor.ExistingPhotoPath); System.IO.File.Delete(filePath); } updateContractor.Image = ProcessUploadFile(contractor); } db.Update(updateContractor); await db.SaveChangesAsync(); TempData["Message"] = "Data Updated Successfully"; TempData["Status"] = "2"; return(RedirectToAction(nameof(Index))); } ViewData["CityId"] = new SelectList(db.Cities, "CityId", "Name", contractor.CityId); return(View(contractor)); }
// GET: Contractors/Delete/5 public async Task <IActionResult> Delete(int?id) { if (id == null) { return(NotFound()); } var contractor = await db.Contractors .Include(c => c.City) .FirstOrDefaultAsync(m => m.ContractorId == id); ContractorEditViewModels contractorEditView = new ContractorEditViewModels { Id = contractor.ContractorId, Name = contractor.Name, Email = contractor.Email, PrintEmail = contractor.PrintEmail, Contact = contractor.Contact, Address = contractor.Address, ExistingPhotoPath = contractor.Image, CityId = contractor.CityId }; ViewBag.Title = "Delete"; ViewData["CityId"] = new SelectList(db.Cities, "CityId", "Name", contractor.CityId); return(View("Edit", contractorEditView)); }
//// GET: Contractors/Create //public IActionResult Create() //{ // ViewData["CityId"] = new SelectList(db.Cities, "CityId", "Name"); // ViewBag.Title = "Create"; // return View(); //} //// POST: Contractors/Create //[HttpPost] //[ValidateAntiForgeryToken] //public async Task<IActionResult> Create( ContractorViewModels contractor) //{ // //if (ModelState.IsValid) // //{ // // if(contractor.ContractorId>0) // // { // // db.Update(contractor); // // await db.SaveChangesAsync(); // // TempData["Message"] = "Data Update Successfully"; // // TempData["Status"] = "2"; // // } // // else // // { // // try // // { // // db.Add(contractor); // // await db.SaveChangesAsync(); // // TempData["Message"] = "Data Added Successfully"; // // TempData["Status"] = "1"; // // //ViewBag.Message = "Data Added Successfully"; // // //ViewBag.Status = "1"; // // } // // catch (DbUpdateConcurrencyException) // // { // // if (!ContractorExists(contractor.ContractorId)) // // { // // return NotFound(); // // } // // else // // { // // throw; // // } // // } // // } // // return RedirectToAction(nameof(Index)); // //} // if (ModelState.IsValid) // { // string uniqueFileName = ProcessUploadFile(contractor); // Contractor newContractor = new Contractor // { // Name = contractor.Name, // Email = contractor.Email, // PrintEmail = contractor.PrintEmail, // Contact = contractor.Contact, // Address = contractor.Address, // Image = uniqueFileName, // CityId = contractor.CityId // }; // db.Add(newContractor); // await db.SaveChangesAsync(); // TempData["Message"] = "Data Added Successfully"; // TempData["Status"] = "1"; // return RedirectToAction(nameof(Index)); // } // ViewData["CityId"] = new SelectList(db.Cities, "CityId", "Name", contractor.CityId); // return View(contractor); //} // GET: Contractors/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var contractor = await db.Contractors.FindAsync(id); if (contractor == null) { return(NotFound()); } ContractorEditViewModels contractorEdit = new ContractorEditViewModels { Id = contractor.ContractorId, Name = contractor.Name, Email = contractor.Email, PrintEmail = contractor.PrintEmail, Contact = contractor.Contact, Address = contractor.Address, CityId = contractor.CityId, ExistingPhotoPath = contractor.Image }; ViewData["CityId"] = new SelectList(db.Cities, "CityId", "Name", contractor.CityId); return(View("Edit", contractorEdit)); }