public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Usher usher = db.Ushers.Find(id); if (usher == null) { return(HttpNotFound()); } var model = new UsherViewModel { UsherId = usher.UsherId, FirstName = usher.FirstName, LastName = usher.LastName, MobileNumber = usher.MobileNumber, DateOfBirth = usher.DateOfBirth, Gender = usher.Gender, Nationality = usher.Nationality, City = usher.City, CarAvailability = usher.CarAvailability, MedicalCard = usher.MedicalCard, Language = usher.Language.Name, }; return(View(model)); }
public ActionResult Edit(UsherViewModel model) { if (ModelState.IsValid) { // Validating the date of birth if (model.DateOfBirth > DateTime.Today.AddYears(-15)) { // The message will be displayed in @Html.ValidationMessageFor(m => m.CreationDate) ModelState.AddModelError("DateOfBirth", "The usher should be 16 years old or above. "); // The message will be displayed in @Html.ValidationSummary() ModelState.AddModelError(String.Empty, "Issue with the date of birth"); return(View(model)); } Usher usher = Mapper.Map <UsherViewModel, Usher>(model); db.Entry(usher).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.LanguageId = new SelectList(db.Languages, "Id", "Name", model.LanguageId); return(View(model)); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Usher usher = db.Ushers.Find(id); if (usher == null) { return(HttpNotFound()); } UsherViewModel model = Mapper.Map <Usher, UsherViewModel>(usher); ViewBag.LanguageId = new SelectList(db.Languages, "Id", "Name", usher.LanguageId); return(View(model)); }
public ActionResult Create(UsherViewModel model) { if (ModelState.IsValid) { ViewBag.languageId = new SelectList(db.Languages, "Id", "Name"); var usher = new Usher { FirstName = model.FirstName, LastName = model.LastName, MobileNumber = model.MobileNumber, DateOfBirth = model.DateOfBirth, Gender = model.Gender, Nationality = model.Nationality, City = model.City, CarAvailability = model.CarAvailability, MedicalCard = model.MedicalCard, LanguageId = model.LanguageId, }; // Validating the date of birth if (model.DateOfBirth > DateTime.Today.AddYears(-15)) { // The message will be displayed in @Html.ValidationMessageFor(m => m.CreationDate) ModelState.AddModelError("DateOfBirth", "The usher should be 16 years old or above. "); // The message will be displayed in @Html.ValidationSummary() ModelState.AddModelError(String.Empty, "Issue with the date of birth"); return(View(model)); } //TODO Remove invalid characters from the filename such as white spaces // check if the uploaded file is empty if (model.MedicalCardFile != null && model.MedicalCardFile.ContentLength > 0) { // Allowed extensions to be uploaded var extensions = new[] { "pdf", "docx", "doc", "jpg", "jpeg", "png" }; // Get the file name without the path string filename = Path.GetFileName(model.MedicalCardFile.FileName); // Get the extension of the file string ext = Path.GetExtension(filename).Substring(1); // Check if the extension of the file is in the list of allowed extensions if (!extensions.Contains(ext, StringComparer.OrdinalIgnoreCase)) { ModelState.AddModelError(string.Empty, "Accepted file are pdf, docx, doc, jpg, jpeg, and png documents"); return(View()); } // Set the application folder where to save the uploaded file string appFolder = "~/Content/Uploads/"; // Generate a random string to add to the file name // This is to avoid the files with the same names var rand = Guid.NewGuid().ToString(); // Combine the application folder location with the file name string path = Path.Combine(Server.MapPath(appFolder), rand + "-" + filename); // Save the file in ~/Content/Uploads/filename.xyz model.MedicalCardFile.SaveAs(path); // Add the path to the course object usher.MedicalCard = appFolder + rand + "-" + filename; } else { ModelState.AddModelError(string.Empty, "Empty files are not accepted"); ViewBag.languageId = new SelectList(db.Languages, "Id", "Name"); return(View()); } db.Ushers.Add(usher); db.SaveChanges(); ViewBag.languageId = new SelectList(db.Languages, "Id", "Name"); return(RedirectToAction("Index")); } ViewBag.languageId = new SelectList(db.Languages, "Id", "Name"); return(View(model)); }