public ActionResult AddCandidate(AddCandidateView view) { ViewBag.userId = new SelectList(db.Users.OrderBy(g => g.firtsName).ThenBy(g => g.lastName), "userId", "fullName"); if (!ModelState.IsValid) { return(View(view)); } var candidate = db.Candidates.Where(vg => vg.votingId == view.votingId && vg.userId == view.userId).FirstOrDefault(); if (candidate != null) { ViewBag.Error = "The user already is candidate to this voting"; return(View(view)); } var candidateUser = new Candidate { votingId = view.votingId, userId = view.userId, }; db.Candidates.Add(candidateUser); db.SaveChanges(); return(RedirectToAction(string.Format("Details/{0}", view.votingId))); }
public ActionResult AddCandidate(AddCandidateView view) { if (ModelState.IsValid) { var candidate = db.Candidates.Where(c => c.VotingId == view.VotingId && c.StudentID == view.UserId).FirstOrDefault(); if (candidate != null) { ModelState.AddModelError(string.Empty, "The Candidate already belongs to voting..."); ViewBag.Candidates = new SelectList(db.Students, "StudentID", "LastName"); return(View(view)); } candidate = new Candidate { StudentID = view.UserId, VotingId = view.VotingId, }; db.Candidates.Add(candidate); db.SaveChanges(); return(RedirectToAction(string.Format("Details/{0}", view.VotingId))); } ViewBag.StudentID = new SelectList(db.Students.OrderBy(u => u.FirstName).ThenBy(u => u.LastName), "StudentID", "FullName"); return(View(view)); }
public ActionResult AddCandidate(AddCandidateView view) { if (ModelState.IsValid) { var candidate = db.Candidates .Where(c => c.VotingId == view.VotingId && c.UserId == view.UserId) .FirstOrDefault(); //Si en caso ya esta asignado ese candidato al voting if (candidate != null) { ModelState.AddModelError(string.Empty, "The candidate already belong to voting"); //ViewBag.Error = ""; ViewBag.UserId = new SelectList(db.Users.OrderBy(u => u.FirstName).ThenBy(u => u.LastName), "UserId", "FullName"); return(View(view)); } candidate = new Candidate() { VotingId = view.VotingId, UserId = view.UserId }; db.Candidates.Add(candidate); db.SaveChanges(); return(RedirectToAction("Details", new { id = view.VotingId })); //return RedirectToAction(string.Format("Details/{0}",view.VotingId)); } ViewBag.UserId = new SelectList(db.Users.OrderBy(u => u.FirstName).ThenBy(u => u.LastName), "UserId", "FullName"); return(View(view)); }
public ActionResult AddCandidate(int id) { var view = new AddCandidateView { VotingId = id }; ViewBag.UserId = new SelectList(db.Users.OrderBy(u => u.FirstName).ThenBy(u => u.LastName), "UserId", "FullName"); return(View(view)); }
public ActionResult AddCandidate(int id) { ViewBag.userId = new SelectList(db.Users.OrderBy(g => g.firtsName).ThenBy(g => g.lastName), "userId", "fullName"); var view = new AddCandidateView { votingId = id, }; return(View(view)); }
public ActionResult AddCandidate(int id) { var view = new AddCandidateView { VotingId = id, }; ViewBag.Candidates = new SelectList(db.Students, "StudentID", "LastName"); Student student = db.Students.Find(id); return(View(view)); }
public ActionResult AddCandidate(int id) { if (id == 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var addCandidate = new AddCandidateView { VotingId = id }; ViewBag.UserId = new SelectList(db.Users.OrderBy(p => p.FirstName).ThenBy(p => p.LastName), "UserId", "FullName"); return(View(addCandidate)); }
public ActionResult AddCandidate(AddCandidateView view) { if (ModelState.IsValid) { var candidate = db.Candidates .Where(c => c.VotingId == view.VotingId && c.UserId == view.UserId) .FirstOrDefault(); if (candidate != null) { ModelState.AddModelError(string.Empty, "The candidate already belongs to voting"); ViewBag.UserId = new SelectList( db.Users.OrderBy(g => g.FirstName).ThenBy(g => g.LastName), "UserId", "FullName"); return View(view); } candidate = new Candidate { UserId = view.UserId, VotingId = view.VotingId }; db.Candidates.Add(candidate); db.SaveChanges(); return RedirectToAction(string.Format("Details/{0}", candidate.VotingId)); } ViewBag.UserId = new SelectList( db.Users.OrderBy(g => g.FirstName).ThenBy(g => g.LastName), "UserId", "FullName"); return View(view); }
public ActionResult AddCandidate(AddCandidateView view) { if (ModelState.IsValid) { //busco si grupo existe o si ya esta seleccionado: var candidate = db.Candidates.Where(c => c.VotingId == view.VotingId && c.UserId == view.UserId).FirstOrDefault();//ejecuto la funcion link: //Aquí a ver si si lo encontro: if (candidate != null) { ModelState.AddModelError("", "The Candidate already belong to Voting."); //ViewBag.Error = "The group already belong to Voting."; ViewBag.UserId = new SelectList(db.Users.OrderBy(u => u.FirstName).ThenBy(u => u.lastName), "UserId", "FullName").ToList(); return(View(view)); } //Si no existe debo crear el objeto de bd: y despues de crear el objeto lo envio a la base de datos: candidate = new Candidate { UserId = view.UserId, VotingId = view.VotingId, }; db.Candidates.Add(candidate); db.SaveChanges(); return(RedirectToAction($"Details/{view.VotingId}")); } ViewBag.UserId = new SelectList(db.Users.OrderBy(u => u.FirstName).ThenBy(u => u.lastName), "UserId", "FullName").ToList(); return(View(view)); }