コード例 #1
0
        public String Edit(Models.ViewModels.AccountViewModel.EditViewModel model)
        {
            User userInDB;

            using (DbContextTransaction tran = db.Database.BeginTransaction())
            {
                int id;
                try
                {
                    id               = Int32.Parse(Session["userID"].ToString());
                    userInDB         = db.User.Include(u => u.Allergy).First(u => u.Id == id);
                    userInDB.Allergy = new HashSet <Allergy>();
                    if (model.Email != null)
                    {
                        userInDB.Email = model.Email;
                    }
                    if (model.Allergies.Length != 0)
                    {
                        AddAllergies(userInDB, model.Allergies);
                        TempData["allergyIDList"] = model.Allergies.ToList();
                    }
                    db.SaveChanges();
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    id       = Int32.Parse(Session["userID"].ToString());
                    userInDB = db.User.Single(u => u.Id == id);
                }
            }
            ViewData["DefaultAllergies"] = HelperMethods.GetDefaultAllergies(db);
            return("success");
        }
コード例 #2
0
        public ActionResult Edit()
        {
            int  id   = Int32.Parse(Session["userID"].ToString());
            User user = db.User.Where(u => u.Id == id).FirstOrDefault();

            Models.ViewModels.AccountViewModel.EditViewModel model = new Models.ViewModels.AccountViewModel.EditViewModel
            {
                Allergies = new int[user.Allergy.Count],
                Email     = user.Email
            };

            List <Allergy> userAllergyList = user.Allergy.ToList();

            for (int i = 0; i < model.Allergies.Length; i++)
            {
                model.Allergies[i] = userAllergyList[i].Id;
            }

            //ViewDataDictionary needs DefaultAllergies to draw it properly
            ViewData["DefaultAllergies"] = HelperMethods.GetDefaultAllergies(db);
            return(View(model));
        }