public ActionResult Create(Beneficiary beneficiary) { if (ModelState.IsValid) { db.Beneficiaries.Add(beneficiary); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CommunityID = new SelectList(db.Communities, "CommunityID", "Name", beneficiary.CommunityID); ViewBag.TypeOfBeneficiaryID = new SelectList(db.TypeOfBeneficiaries, "TypeOfBeneficiaryID", "Description", beneficiary.TypeOfBeneficiaryID); return View(beneficiary); }
public ActionResult Create(HttpPostedFileBase file, Beneficiary beneficiary) { if (ModelState.IsValid) { if (file == null) { ModelState.AddModelError("File", "Please Upload Your file"); } else if (file.ContentLength > 0) { int MaxContentLength = 1024 * 1024 * 3; //3 MB string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" }; if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.')))) { ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions)); } else if (file.ContentLength > MaxContentLength) { ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB"); } else { //TO:DO var sGuid = Guid.NewGuid().ToString(); var fileName = sGuid + file.FileName.Substring(file.FileName.LastIndexOf('.')); var path = Path.Combine(Server.MapPath("~/Upload/BeneficiaryImages/"), fileName); file.SaveAs(path); ModelState.Clear(); ViewBag.Message = "File uploaded successfully"; beneficiary.ImagePath = "~/Upload/BeneficiaryImages/" + fileName; } if (ModelState.IsValid) { db.Beneficiaries.Add(beneficiary); db.SaveChanges(); return RedirectToAction("Index"); } } } ViewBag.CommunityID = new SelectList(db.Communities, "CommunityID", "Name", beneficiary.CommunityID); ViewBag.TypeOfBeneficiaryID = new SelectList(db.TypeOfBeneficiaries, "TypeOfBeneficiaryID", "Description", beneficiary.TypeOfBeneficiaryID); return View(beneficiary); }