public void Create(JudgeCreationViewModel model) { //adding judge itself if (model != null) { var judgeToAdd = new Judge() { FirstName = model.FirstName, LastName = model.LastName, Bio = model.Bio, Email = model.Email }; _Context.Judges.Add(judgeToAdd); _Context.SaveChanges(); //uploading image var file = model.Upload; var fileName = Guid.NewGuid().ToString(); var extension = Path.GetExtension(file.FileName); var path = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/assets/img"), fileName + extension); var photoToAdd = new Photo(); photoToAdd.JudgeId = judgeToAdd.Id; photoToAdd.Name = fileName; photoToAdd.Extension = extension; photoToAdd.Path = path; file.SaveAs(path); _Context.Photos.Add(photoToAdd); _Context.SaveChanges(); //adding season_judge foreach (var item in model.JudgedSeasonList) { if (item.IsChecked) { var season_JudgeToAdd = new Season_Judge() { JudgeId = judgeToAdd.Id, SeasonId = item.SeasonId }; _Context.Season_Judge.Add(season_JudgeToAdd); _Context.SaveChanges(); } } } }
public ActionResult Create(JudgeCreationViewModel model) { if (ModelState.IsValid) { DP.Create(model); return(RedirectToAction("Judges", "Admin")); } foreach (var item in model.JudgedSeasonList) { item.season = DP.GetSeasons().FirstOrDefault(x => x.Id == item.SeasonId); } return(View(model)); }
public ActionResult Create() { var seasons = DP.GetSeasons(); var model = new JudgeCreationViewModel(); model.JudgedSeasonList = new List <JudgedSeason>(); foreach (Season item in seasons) { model.JudgedSeasonList.Add(new JudgedSeason() { IsChecked = false, SeasonId = item.Id, season = item }); } return(View(model)); }