public ActionResult ManageTeamEvents(string message) { CoachAccessRepository coachRep = new CoachAccessRepository(); UserAccessRepository userRep = new UserAccessRepository(); CoachManageTeamEventsView CoachTeamEventsView = new CoachManageTeamEventsView(); Guid CoachID = userRep.GetPersonID(System.Web.HttpContext.Current.User.Identity.Name); CoachTeamEventsView.ExistingEvents = coachRep.GetEvents(CoachID); List <Team> teams = coachRep.GetTeams(CoachID); if (teams.Count != 0) { CoachTeamEventsView.TeamsToNotify = new List <Views.Shared.CheckBoxListInfo>(); for (int i = 0; i < teams.Count; i++) { CoachTeamEventsView.TeamsToNotify.Add(new CheckBoxListInfo(teams.ElementAt(i).Id.ToString(), teams.ElementAt(i).Name, false)); } } if (message != null) { ViewData["Message"] = message; } return(View(CoachTeamEventsView)); }
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ModelStateDictionary mState = bindingContext.ModelState; System.Collections.Specialized.NameValueCollection form = controllerContext.HttpContext.Request.Form; CoachManageTeamEventsView coachManageTeamsEventsView = new CoachManageTeamEventsView(); if (form["Event.Name"] == "") { mState.AddModelError("Name", "Name is required"); } if (form["Event.ScheduledDate"] == "") { mState.AddModelError("ScheduledDate", "Scheduled date is required"); } if (form["Event.Description"] == "") { mState.AddModelError("Description", "Description is required"); } if (form["Event.Location"] == "") { mState.AddModelError("Location", "Location is required"); } if (form["HasSelectedTeam"] != null) { coachManageTeamsEventsView.HasSelectedTeam = bool.Parse(form["HasSelectedTeam"]); } if (coachManageTeamsEventsView.HasSelectedTeam == false) { mState.AddModelError("HasSelectedTeam", "Please select at least one available team."); } if (mState.Count == 0) { coachManageTeamsEventsView.Event = new Event(Guid.NewGuid()); coachManageTeamsEventsView.Event.Name = form["Event.Name"]; coachManageTeamsEventsView.Event.Location = form["Event.Location"]; coachManageTeamsEventsView.Event.ScheduledDate = DateTime.Parse(form["Event.ScheduledDate"]); coachManageTeamsEventsView.Event.Description = form["Event.Description"]; coachManageTeamsEventsView.SendEmailToPlayers = form["SendEmailToPlayers"].ToString().Equals("false") ? false : true; if (form.AllKeys.Length > 5) { coachManageTeamsEventsView.TeamsToNotify = new List <CheckBoxListInfo>(); for (int i = 3; i < form.AllKeys.Length - 3; i++) { if (form.AllKeys[i].Substring(0, form.AllKeys[i].IndexOf('[')).Contains("Teams")) { CheckBoxListInfo chk = new CheckBoxListInfo(); chk.Value = form[form.AllKeys[i]]; coachManageTeamsEventsView.TeamsToNotify.Add(chk); } } } } return(coachManageTeamsEventsView); }
public ActionResult ManageTeamEvents([ModelBinder(typeof(CoachManageTeamEventsViewModelBinder))] CoachManageTeamEventsView CoachManageTeamEventsView) { CoachAccessRepository coachRep = new CoachAccessRepository(); UserAccessRepository userRep = new UserAccessRepository(); Guid CoachID = userRep.GetPersonID(System.Web.HttpContext.Current.User.Identity.Name); List <Team> teams = new List <Team>(); for (int i = 0; i < CoachManageTeamEventsView.TeamsToNotify.Count; i++) { Team team = coachRep.GetTeam(Guid.Parse(CoachManageTeamEventsView.TeamsToNotify[i].Value)); teams.Add(team); } if (ModelState.IsValid) { CoachManageTeamEventsView.Event.CreatedBy = CoachID; CoachManageTeamEventsView.Event.Teams = teams; if (coachRep.CreateEvent(CoachManageTeamEventsView.Event)) { ViewData["Message"] = "Your event was created successfully."; if (CoachManageTeamEventsView.SendEmailToPlayers) { List <Person> people = new List <Person>(); foreach (var team in CoachManageTeamEventsView.Event.Teams) { List <Person> players = coachRep.GetTeamPlayers(team.Id); people.AddRange(players); } MailMessage message = new MailMessage(); //message.To.Add(new MailAddress(PlayerApplication.Player.Email)); message.Subject = "PhoenixFC - Event - " + CoachManageTeamEventsView.Event.Name; message.Body = "Dear Player: Your coach has scheduled an event. Please check your team web page for more information."; SmtpClient client = new SmtpClient(); foreach (var person in people) { message.To.Add(new MailAddress(person.Email)); } try { client.Send(message); return(RedirectToAction("ManageTeamEvents", "Coach", new { message = "Your event was created successfully." })); } catch (Exception e) { if (e is System.Net.Mail.SmtpException) { return(RedirectToAction("ManageTeamEvents", "Coach", new { message = "Your event was created successfully. However, there was an error sending email to all players." })); } } } CoachManageTeamEventsView.Event = null; CoachManageTeamEventsView.SendEmailToPlayers = false; CoachManageTeamEventsView.ExistingEvents = coachRep.GetEvents(CoachID); } else { return(RedirectToAction("ManageTeamEvents", "Coach", new { message = "There was an error creating your event. Please contact your administrator." })); } } teams = coachRep.GetTeams(CoachID); if (teams.Count != 0) { CoachManageTeamEventsView.TeamsToNotify = new List <Views.Shared.CheckBoxListInfo>(); for (int i = 0; i < teams.Count; i++) { CoachManageTeamEventsView.TeamsToNotify.Add(new CheckBoxListInfo(teams.ElementAt(i).Id.ToString(), teams.ElementAt(i).Name, false)); } } return(View(CoachManageTeamEventsView)); }