public ActionResult AssoEdit(short?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Locale locale = db.Locales.Find(id);

            if (locale == null)
            {
                return(HttpNotFound());
            }

            ViewBag.Majs = db.Races.OrderBy(r => r.RaceName).ToList();
            var            selected = db.Majorities.Where(t => t.LocaleId == locale.LocaleId).ToList();
            List <AssoMaj> assoMajs = new List <AssoMaj>();

            foreach (Majority majority in selected)
            {
                AssoMaj toAdd = new AssoMaj(majority);
                assoMajs.Add(toAdd);
            }

            ViewBag.Events = db.Events.OrderBy(e => e.Name).ToList();
            var selected2 = db.LocaleEvents.Where(i => i.LocaleId == locale.LocaleId).ToList();
            List <AssoEvent_Locale> assoEvents = new List <AssoEvent_Locale>();

            foreach (LocaleEvent localeEvent in selected2)
            {
                AssoEvent_Locale toAdd = new AssoEvent_Locale(localeEvent);
                assoEvents.Add(toAdd);
            }

            var          infoId = locale.InfoId;
            AssoLocaleVM model  = new AssoLocaleVM {
                InfoId = infoId, LocaleId = locale.LocaleId, Submit = "Save", AssoMajs = assoMajs, AssoEvents = assoEvents, Name = locale.Name
            };

            return(View(model));
        }
        public ActionResult AssoEdit(short infoId, short localeId, string submit,
                                     List <AssoMaj> majs,
                                     List <AssoEvent_Locale> events)
        {
            if (ModelState.IsValid)
            {
                #region Add majorities
                List <byte> currentMajId = db.Majorities.Where(x => x.LocaleId == localeId).Select(x => x.RaceId).ToList();
                if (majs != null)
                {
                    foreach (AssoMaj maj in majs)
                    {
                        //if the association already exists
                        if (currentMajId.Contains(maj.RaceId))
                        {
                            Majority toEdit = db.Majorities.Where(x => x.RaceId == maj.RaceId && x.LocaleId == localeId).First();
                            toEdit.IsFirst         = maj.IsFirst;
                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentMajId.Remove(maj.RaceId);
                        }
                        else
                        {
                            Majority toAdd = new Majority
                            {
                                LocaleId = localeId,
                                IsFirst  = maj.IsFirst,
                                RaceId   = maj.RaceId
                            };
                            db.Majorities.Add(toAdd);
                            db.SaveChanges();
                        }
                    }
                }
                if (currentMajId.Count != 0)
                {
                    foreach (byte id in currentMajId)
                    {
                        Majority gone = db.Majorities.Where(x => x.LocaleId == localeId && x.RaceId == id).FirstOrDefault();
                        db.Majorities.Remove(gone);
                        db.SaveChanges();
                    }
                }
                #endregion

                #region Add LocaleEvents
                List <short> currentEventId = db.LocaleEvents.Where(x => x.LocaleId == localeId).Select(x => x.EventId).ToList();
                if (events != null)
                {
                    foreach (AssoEvent_Locale assoEvent in events)
                    {
                        //if the association already exists
                        if (currentEventId.Contains(assoEvent.EventId))
                        {
                            LocaleEvent toEdit = db.LocaleEvents.Where(x => x.LocaleId == localeId && x.EventId == assoEvent.EventId).First();
                            //if I ever add more columns to LocaleEvent, edit them here
                            //db.Entry(toEdit).State = EntityState.Modified;
                            //db.SaveChanges();
                            currentEventId.Remove(assoEvent.EventId);
                        }
                        else
                        {
                            LocaleEvent toAdd = new LocaleEvent
                            {
                                LocaleId = localeId,
                                EventId  = assoEvent.EventId
                            };
                            db.LocaleEvents.Add(toAdd);
                        }
                    }
                }
                if (currentEventId.Count != 0)
                {
                    foreach (short id in currentEventId)
                    {
                        LocaleEvent gone = db.LocaleEvents.Where(x => x.LocaleId == localeId && x.EventId == id).FirstOrDefault();
                        db.LocaleEvents.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion
                return(Json(true));
            }
            //having a lot of trouble with the return so the ajax right now
            ViewBag.Majs   = db.Races.OrderBy(r => r.RaceName).ToList();
            ViewBag.Events = db.Events.OrderBy(e => e.Name).ToList();
            var          locale = db.Locales.Find(localeId);
            AssoLocaleVM model  = new AssoLocaleVM {
                InfoId = infoId, LocaleId = localeId, Submit = submit, AssoMajs = majs, AssoEvents = events, Name = locale.Name
            };
            return(View(model));
        }