public ActionResult Create(ProgramType programtype) { if (ModelState.IsValid) { db.ProgramTypes.Add(programtype); try { db.SaveChanges(); } catch (Exception e) { Session["FlashMessage"] = "Failed to create type." + e.Message; List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Application End Time", Value = "deadline", Selected = true }); items.Add(new SelectListItem { Text = "Program Date", Value = "program" }); ViewBag.display_date = items; return View(programtype); } return RedirectToAction("Index"); } return View(programtype); }
public ActionResult Edit(ProgramType programtype) { if (ModelState.IsValid) { if (!String.IsNullOrEmpty(programtype.image_filename)) { var sourcePath = Server.MapPath("~/App_Data/" + programtype.image_filepath); var sourceFilepath = Path.Combine(sourcePath, programtype.image_filename); var destPath = Server.MapPath("~/Images/ProgramType/" + programtype.id.ToString()); var destFilepath = Path.Combine(destPath, programtype.image_filename); try { Directory.CreateDirectory(destPath); } catch (Exception e) { Session["FlashMessage"] = "Failed to create directory. Please check write permission of ~/Images/ProgramType. <br/><br/>" + e.Message; } try { System.IO.File.Move(sourceFilepath, destFilepath); programtype.image_filepath = "~/Images/ProgramType/" + programtype.id.ToString(); } catch (Exception e) { Session["FlashMessage"] = "Failed to move file. Please check write permission of ~/Images/ProgramType. <br/><br/>" + e.Message; } } //clear temp files uploaded but not used if (Directory.Exists(Server.MapPath("~/App_Data/Temp/ProgramType/" + programtype.id.ToString()))) { var files = Directory.GetFiles(Server.MapPath("~/App_Data/Temp/ProgramType/" + programtype.id.ToString())); foreach (var file in files) { System.IO.File.Delete(file); } } db.Entry(programtype).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception e) { Session["FlashMessage"] = "Failed to update type." + e.Message; List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "Application End Time", Value = "deadline", Selected = true }); items.Add(new SelectListItem { Text = "Program Date", Value = "program" }); ViewBag.display_date = items; return View(programtype); } return RedirectToAction("Index"); } return View(programtype); }