Пример #1
0
        public ActionResult Create(Candidate candidate, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                candidate.Candidate_ImagePath = Path.GetExtension(file.FileName);
                candidate.Candidate_Image     = ConvertToBytes(file);
            }

            //if (ModelState.IsValid)
            //{
            candidate.Candidate_ID         = candidate.GenerateCandID();
            candidate.Candidate_TotalVotes = 0;

            var admin = (string)Session["Admin_Username"];

            candidate.Admin_Username = admin;

            if (db.Candidates.Any(x => x.Candidate_ID == candidate.Candidate_ID) || db.Candidates.Any(x => x.Candidate_StudentNumber == candidate.Candidate_StudentNumber))
            {
                ModelState.Clear();
                ViewBag.DuplicateMessage = "This candidate already exists.";
                return(View("Create"));
            }


            var studentNum = db.MockStudents.Find(candidate.Candidate_StudentNumber);

            if (studentNum == null)
            {
                ModelState.Clear();
                ViewBag.ErrorMessage = "Invalid candidate! Not a registered DUT student!";
                return(View("Create", candidate));
            }


            var party = db.Parties.Find(candidate.Party_Name);

            if (party != null)
            {
                party.Party_NoOfCandidates += 1;
                db.Entry(party).State       = EntityState.Modified;
                db.SaveChanges();
            }

            else if (party == null)
            {
                ModelState.Clear();
                ViewBag.ErrorMessage = "Incorrect Party!";
                return(View("Create", candidate));
            }


            db.Candidates.Add(candidate);
            db.SaveChanges();

            ModelState.Clear();
            ViewBag.SuccessMessage = "Registration Successfully.";
            return(View("Create", new Candidate()));
        }