// GET: NPCs/Edit/5 public ActionResult Edit(short?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } NPC npc = db.NPCs.Find(id); if (npc == null) { return(HttpNotFound()); } ViewBag.LastLocationId = new SelectList(db.Locales, "LocaleId", "Name", npc.LastLocationId); ViewBag.RaceId = new SelectList(db.Races, "RaceId", "RaceName", npc.RaceId); ViewBag.GenderId = new SelectList(db.Genders, "GenderId", "GenderName", npc.GenderId); short infoid = npc.InfoId; Info info = db.Infos.Find(infoid); NpcEditVM model = new NpcEditVM(npc, 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)); }
public ActionResult Edit([Bind(Include = "NpcId,InfoId,Name,Alias,Quote,PortraitFileName,RaceId,CrestFileName,ApperanceText,AboutText,LastLocationId,PortraitArtist,CrestArtist,IsDead,GenderId, Blurb, IsSecret")] NpcEditPostVM npc, List <short> tags, HttpPostedFileBase portraitPic, HttpPostedFileBase crestPic, string submit) { #region Pre-model picture check if (portraitPic != null) { string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" }; string imgName = portraitPic.FileName; string ext = imgName.Substring(imgName.LastIndexOf('.')); if (!goodExts.Contains(ext.ToLower())) { ModelState.AddModelError("PortraitFileName", "You have submitted a incorrect file type for your portrait. Please use either: .jpg, .jpeg, .gif, or .png"); } if (npc.PortraitArtist == null) { ModelState.AddModelError("PortraitArtist", "Katherine, you're trying to submit something with a picture without an artist. That's a no-no! But seriously, if something came up that means you need to change this rule, you know who to call."); } } else if ((npc.PortraitFileName != "default.jpg" && npc.PortraitFileName != null) && npc.PortraitArtist == null) { ModelState.AddModelError("PortraitArtist", "Yo bud, you tired? Seems you deleted the artist by accident. Why don't ya fix that?"); } if (crestPic != null) { string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" }; string imgName = crestPic.FileName; string ext = imgName.Substring(imgName.LastIndexOf('.')); if (!goodExts.Contains(ext.ToLower())) { ModelState.AddModelError("CrestFileName", "You have submitted a incorrect file type for your portrait. Please use either: .jpg, .jpeg, .gif, or .png"); } if (npc.CrestArtist == null) { ModelState.AddModelError("CrestArtist", "Katherine, you're trying to submit something with a picture without an artist. That's a no-no! But seriously, if something came up that means you need to change this rule, you know who to call."); } } else if ((npc.CrestFileName != "org_default.jpg" && npc.CrestFileName != null) && npc.CrestArtist == null) { ModelState.AddModelError("CrestArtist", "Yo bud, you tired? Seems you deleted the artist by accident. Why don't ya fix that?"); } #endregion if (ModelState.IsValid) { #region Save or Publish? switch (submit) { case "Save Progress": case "Un-Publish": npc.IsPublished = false; break; case "Publish": case "Save": npc.IsPublished = true; break; case "Save and go to complex edit": break; } #endregion var infoid = npc.InfoId; #region Info Update //Info info = db.Infos.Find(infoid); Info info = db.Infos.Where(i => i.InfoId == infoid).FirstOrDefault(); info.Name = npc.Name; info.Blurb = npc.Blurb; info.IsPublished = npc.IsPublished; info.IsSecret = npc.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 Image Update/Upload string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" }; #region Image Update -Portrait if (portraitPic != null) { string imgName = portraitPic.FileName; string ext = imgName.Substring(imgName.LastIndexOf('.')); if (goodExts.Contains(ext.ToLower())) { imgName = "npc-portrait-" + infoid.ToString() + ext; portraitPic.SaveAs(Server.MapPath("~/Content/img/npc/" + imgName)); } //remove old picture if it had a different extension (and thus would not be overridden) string oldName = npc.PortraitFileName; string oldExt = oldName.Substring(oldName.LastIndexOf('.')); if (oldName != "default.jpg" && oldExt != ext) { string fullPath = Request.MapPath("~/Content/img/npc/" + oldName); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } } //assign new PortraitFileName npc.PortraitFileName = imgName; } #endregion #region Image Update -Crest if (crestPic != null) { string imgName = crestPic.FileName; string ext = imgName.Substring(imgName.LastIndexOf('.')); if (goodExts.Contains(ext.ToLower())) { imgName = "npc-crest-" + infoid.ToString() + ext; crestPic.SaveAs(Server.MapPath("~/Content/img/npc/" + imgName)); } //remove old picture if it had a different extension (and thus would not be overridden) string oldName = npc.CrestFileName; string oldExt = oldName.Substring(oldName.LastIndexOf('.')); if (oldName != "org_default.jpg" && oldExt != ext) { string fullPath = Request.MapPath("~/Content/img/npc/" + oldName); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } } //assign new CrestFileName npc.CrestFileName = imgName; } #endregion #endregion #region Update Npc NPC daNpc = new NPC { NpcId = npc.NpcId, InfoId = npc.InfoId, Name = npc.Name, Alias = npc.Alias, Quote = npc.Quote, PortraitFileName = npc.PortraitFileName, RaceId = npc.RaceId, CrestFileName = npc.CrestFileName, ApperanceText = npc.ApperanceText, AboutText = npc.AboutText, LastLocationId = npc.LastLocationId, IsPublished = npc.IsPublished, PortraitArtist = npc.PortraitArtist, CrestArtist = npc.CrestArtist, IsDead = npc.IsDead, GenderId = npc.GenderId }; db.Entry(daNpc).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 = npc.NpcId })); } return(RedirectToAction("Details", new { id = npc.NpcId })); } #region if model invalid ViewBag.LastLocationId = new SelectList(db.Locales, "LocaleId", "Name", npc.LastLocationId); ViewBag.RaceId = new SelectList(db.Races, "RaceId", "RaceName", npc.RaceId); ViewBag.GenderId = new SelectList(db.Genders, "GenderId", "GenderName", npc.GenderId); ViewBag.Tags = db.Tags.ToList(); if (tags != null) { ViewBag.Selected = tags; } else { ViewBag.Selected = new List <short>(); } if (portraitPic != null) { ModelState.AddModelError("PortraitFileName", "Hey, there was some error, so you have to re-upload the picture"); } if (crestPic != null) { ModelState.AddModelError("CrestFileName", "Hey, there was some error, so you have to re-upload the picture"); } ModelState.AddModelError("", "Something has gone wrong. Look for red text to see where is went wrong"); #endregion NpcEditVM aNpc = new NpcEditVM(npc); return(View(aNpc)); }