Ejemplo n.º 1
0
        // GET: Rifts/Edit/5
        public ActionResult Edit(short?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Rift rift = db.Rifts.Find(id);

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

            short      infoid = rift.InfoId;
            Info       info   = db.Infos.Find(infoid);
            RiftEditVM model  = new RiftEditVM(rift, info);

            List <short> selectedTags = db.InfoTags.Where(t => t.InfoId == infoid).Select(t => t.TagId).ToList();

            ViewBag.Selected = selectedTags;
            ViewBag.Tags     = db.Tags.ToList();

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Edit([Bind(Include = "RiftId,InfoId,Nickname,Location,Environment,Hazards,Blurb, IsPublished, IsSecret")] RiftEditPostVM rift,
                                 List <short> tags,
                                 string submit)
        {
            if (ModelState.IsValid)
            {
                #region Save or Publish?
                switch (submit)
                {
                case "Save Progress":
                case "Un-Publish":
                    rift.IsPublished = false;
                    break;

                case "Publish":
                case "Save":
                    rift.IsPublished = true;
                    break;

                case "Save and go to complex edit":
                    break;
                }
                #endregion

                var infoid = rift.InfoId;
                #region Info Update
                //Info info = db.Infos.Find(infoid);
                Info info = db.Infos.Where(i => i.InfoId == infoid).FirstOrDefault();
                info.Name        = rift.Nickname;
                info.Blurb       = rift.Blurb;
                info.IsPublished = rift.IsPublished;
                info.IsSecret    = rift.IsSecret;
                #endregion

                #region Update tags
                List <short> currentTagIds = db.InfoTags.Where(x => x.InfoId == infoid).Select(x => x.TagId).ToList();

                if (tags != null)
                {
                    foreach (short tag in tags)
                    {
                        //if this is an already existing tag
                        if (currentTagIds.Contains(tag))
                        {
                            currentTagIds.Remove(tag);
                        }
                        //if this is a newly added tag
                        else
                        {
                            InfoTag newTag = new InfoTag {
                                InfoId = infoid, TagId = tag
                            };
                            db.InfoTags.Add(newTag);
                        }
                    }
                }

                if (currentTagIds.Count != 0)
                {
                    foreach (short id in currentTagIds)
                    {
                        InfoTag gone = db.InfoTags.Where(x => x.InfoId == infoid & x.TagId == id).FirstOrDefault();
                        db.InfoTags.Remove(gone);
                    }
                }

                #endregion

                #region Update Rift
                Rift daRift = new Rift
                {
                    InfoId      = rift.InfoId,
                    RiftId      = rift.RiftId,
                    Nickname    = rift.Nickname,
                    Location    = rift.Location,
                    Environment = rift.Environment,
                    Hazards     = rift.Hazards,
                    IsPublished = rift.IsPublished
                };
                db.Entry(daRift).State = EntityState.Modified;
                db.Entry(info).State   = EntityState.Modified;
                db.SaveChanges();
                #endregion
                if (submit == "Save and go to complex edit")
                {
                    return(RedirectToAction("AssoEdit", new { id = rift.RiftId }));
                }
                return(RedirectToAction("Details", new { id = rift.RiftId }));
            }

            //if model invalid
            ViewBag.Tags = db.Tags.ToList();
            if (tags != null)
            {
                ViewBag.Selected = tags;
            }
            else
            {
                ViewBag.Selected = new List <short>();
            }
            ModelState.AddModelError("", "Something has gone wrong. Look for red text to see where is went wrong");
            RiftEditVM aRift = new RiftEditVM(rift);
            return(View(aRift));
        }