public CoachWithDetail CoachAdd(CoachCreate newItem) { //fetch associated team first var team = ds.Teams.Find(newItem.TeamId); if (team == null) { return(null); } var addedCoach = ds.Coaches.Add(Mapper.Map <Coach>(newItem)); byte[] photobytes = new byte[newItem.PhotoUpload.ContentLength]; newItem.PhotoUpload.InputStream.Read(photobytes, 0, newItem.PhotoUpload.ContentLength); addedCoach.Photo = photobytes; addedCoach.PhotoContentType = newItem.PhotoUpload.ContentType; addedCoach.CoachProfile = newItem.CoachProfile; team.Coaches.Add(addedCoach); ds.SaveChanges(); return((addedCoach == null) ? null : Mapper.Map <CoachWithDetail>(addedCoach)); }
public ActionResult AddCoach(CoachCreate newCoach) { if (!ModelState.IsValid) { var errors = ModelState.Where(x => x.Value.Errors.Any()) .Select(x => new { x.Key, x.Value.Errors }); return(View(newCoach)); } var addedCoach = m.CoachAdd(newCoach); if (addedCoach == null) { return(View(newCoach)); } else { return(RedirectToAction("Details", "Coach", new { id = addedCoach.CoachId })); } }