Ejemplo n.º 1
0
        public ActionResult AssoEdit(short?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Event taevent = db.Events.Find(id);

            if (taevent == null)
            {
                return(HttpNotFound());
            }
            ViewBag.Locales = db.Locales.OrderBy(l => l.Name).ToList();
            var selected = db.LocaleEvents.Where(i => i.EventId == taevent.EventId).ToList();
            List <AssoLocale_Event> assoLocales = new List <AssoLocale_Event>();

            foreach (LocaleEvent locale in selected)
            {
                AssoLocale_Event toAdd = new AssoLocale_Event(locale);
                assoLocales.Add(toAdd);
            }

            ViewBag.Orgs = db.Orgs.OrderBy(l => l.Name).ToList();
            var selected2 = db.OrgEvents.Where(i => i.EventId == taevent.EventId).ToList();
            List <AssoOrg_Event> assoOrgs = new List <AssoOrg_Event>();

            foreach (OrgEvent org in selected2)
            {
                AssoOrg_Event toAdd = new AssoOrg_Event(org);
                assoOrgs.Add(toAdd);
            }

            var         infoid = taevent.InfoId;
            AssoEventVM model  = new AssoEventVM {
                InfoId = infoid, EventId = taevent.EventId, Submit = "Save", Name = taevent.Name, AssoLocales = assoLocales, AssoOrgs = assoOrgs
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult AssoEdit(short infoId, short eventId, string submit,
                                     List <AssoLocale_Event> locales,
                                     List <AssoOrg_Event> orgs)
        {
            if (ModelState.IsValid)
            {
                #region Add Locales
                List <short> currentLocaleId = db.LocaleEvents.Where(x => x.EventId == eventId).Select(x => x.LocaleId).ToList();
                if (locales != null)
                {
                    foreach (AssoLocale_Event le in locales)
                    {
                        //if the association already exists
                        if (currentLocaleId.Contains(le.LocaleId))
                        {
                            LocaleEvent toEdit = db.LocaleEvents.Where(x => x.LocaleId == le.LocaleId && x.EventId == eventId).First();
                            //if I ever add more columns to LocaleEvent, edit them here
                            //db.Entry(toEdit).State = EntityState.Modified;
                            //db.SaveChanges();
                            currentLocaleId.Remove(le.LocaleId);
                        }
                        else
                        {
                            LocaleEvent toAdd = new LocaleEvent
                            {
                                EventId  = eventId,
                                LocaleId = le.LocaleId
                            };
                            db.LocaleEvents.Add(toAdd);
                        }
                    }
                }
                if (currentLocaleId.Count != 0)
                {
                    foreach (short id in currentLocaleId)
                    {
                        LocaleEvent gone = db.LocaleEvents.Where(x => x.EventId == eventId && x.LocaleId == id).FirstOrDefault();
                        db.LocaleEvents.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion

                #region Add Orgs
                List <short> currentOrgId = db.OrgEvents.Where(x => x.EventId == eventId).Select(x => x.OrgId).ToList();
                if (orgs != null)
                {
                    foreach (AssoOrg_Event assoOrg in orgs)
                    {
                        //if the association already exists
                        if (currentOrgId.Contains(assoOrg.OrgId))
                        {
                            OrgEvent toEdit = db.OrgEvents.Where(x => x.OrgId == assoOrg.OrgId && x.EventId == eventId).First();
                            toEdit.Blurb           = assoOrg.Blurb;
                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentOrgId.Remove(assoOrg.OrgId);
                        }
                        else
                        {
                            OrgEvent toAdd = new OrgEvent
                            {
                                EventId = eventId,
                                OrgId   = assoOrg.OrgId,
                                Blurb   = assoOrg.Blurb
                            };
                            db.OrgEvents.Add(toAdd);
                        }
                    }
                }
                if (currentOrgId.Count != 0)
                {
                    foreach (short id in currentOrgId)
                    {
                        OrgEvent gone = db.OrgEvents.Where(x => x.EventId == eventId && x.OrgId == id).FirstOrDefault();
                        db.OrgEvents.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion
                return(Json(true));
            }

            //if model fails
            ViewBag.Locales = db.Locales.OrderBy(l => l.Name).ToList();
            ViewBag.Orgs    = db.Orgs.OrderBy(l => l.Name).ToList();
            var taevent = db.Events.Find(eventId);
            //if I actually was handling if the model failed (as currently I'm using the bandaid solution of just preventing the submition ) this would not cut it. It does not account for orgs or locales being null
            AssoEventVM model = new AssoEventVM {
                InfoId = infoId, EventId = eventId, Submit = submit, Name = taevent.Name, AssoOrgs = orgs, AssoLocales = locales
            };
            return(View(model));
        }