public ActionResult ScorecardDetail(FormCollection collection, ScorecardModel model) { ScorecardModel m = new ScorecardModel(); ScorekeeperDBEntities s = new ScorekeeperDBEntities(); m.SelectedCourseValue = model.SelectedCourseValue; m.SelectedCourse = s.Courses.Where(x => x.CourseId == model.SelectedCourseValue).FirstOrDefault(); int? highestRoundNum = 0; if (s.Rounds.Where(x => x.UserId == m.User.UserId).Count() > 0) highestRoundNum = s.Rounds.Where(x => x.UserId == m.User.UserId).OrderByDescending(x => x.RoundNumber).FirstOrDefault().RoundNumber; Round round = new Round(); round.RoundId = Guid.NewGuid(); round.CourseId = m.SelectedCourse.CourseId; round.UserId = m.User.UserId; round.RoundNumber = highestRoundNum + 1; round.PlayedDate = DateTime.Now; s.AddToRounds(round); for (int i = 0; i < m.SelectedCourse.Hole.Count(); i++) { RoundScore rs = new RoundScore(); rs.RoundScoreId = Guid.NewGuid(); rs.RoundId = round.RoundId; var g = (from h in s.Hole where h.HoleNumber == i+1 && h.CourseId == m.SelectedCourse.CourseId select h.HoleId); rs.HoleId = g.FirstOrDefault(); string holeNum = "Hole" + (i+1).ToString(); rs.Strokes = Int32.Parse(collection[holeNum]); s.AddToRoundScore(rs); } s.SaveChanges(); m.message = "Round Saved Successfully"; m.SelectedCourse = null; return View("Scorecard", m); }
public ActionResult JsonRegister(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; ScorekeeperDBEntities userContext = new ScorekeeperDBEntities(); if (userContext.Users.Where(x => x.Username == model.UserName).Count() == 0) { User u = new Scorekeeper.User(); u.Username = model.UserName; u.Password = model.Password; u.UserId = Guid.NewGuid(); u.FirstName = model.FirstName; u.LastName = model.LastName; userContext.AddToUsers(u); userContext.SaveChanges(); FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false); createStatus = MembershipCreateStatus.Success; } else { createStatus = MembershipCreateStatus.DuplicateUserName; } if (createStatus == MembershipCreateStatus.Success) { return Json(new { success = true }); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed return Json(new { errors = GetErrorsFromModelState() }); }