コード例 #1
0
 public ActionResult Create(Sportsman sportsman, int nationality, int sport)
 {
     if (sportsman != null)
     {
         Nationality nationalityItem = _db.Nationalities.FirstOrDefault(x => x.Id == nationality);
         Sport sportItem = _db.Sport.FirstOrDefault(x => x.Id == sport);
         if (nationalityItem != null && sportItem != null)
         {
             sportsman.Nationality = nationalityItem;
             sportsman.Sport = sportItem;
             _db.Sportsmans.Add(sportsman);
             _db.SaveChanges();
             Success("Your information was saved!");
             return RedirectToAction("Index");
         }
     }
     Error("there were some errors in your form.");
     SetUpViewData();
     return View(sportsman);
 }
コード例 #2
0
        public ActionResult Edit(Sportsman sportsman, int nationality, int sport)
        {
            if (sportsman != null)
            {
                _db.Entry(sportsman).State = EntityState.Modified;
                _db.SaveChanges();

                Nationality nationalityItem = _db.Nationalities.FirstOrDefault(x => x.Id == nationality);
                Sport sportItem = _db.Sport.FirstOrDefault(x => x.Id == sport);
                if (nationalityItem != null && sportItem != null)
                {
                    sportsman = _db.Sportsmans.First(x => x.Id == sportsman.Id);
                    sportsman.Nationality = nationalityItem;
                    sportsman.Sport = sportItem;
                    _db.SaveChanges();
                }
                Success("Your information was saved!");
                return RedirectToAction("Index");
            }
            SetUpViewData();
            return View("Create", new Sportsman());
        }