public ActionResult CreateEducation(CreateEducationViewModel model, HttpPostedFileBase picUpload) { if (Request.IsAuthenticated && ModelState.IsValid) { var user = db.Users.Find(User.Identity.GetUserId()) as ApplicationUser; EntryEducation aEntry = new EntryEducation(); if (picUpload != null && picUpload.ContentLength > 0) { aEntry.Filename = picUpload.FileName; aEntry.ContentType = picUpload.ContentType; using (var reader = new BinaryReader(picUpload.InputStream)) { aEntry.File = reader.ReadBytes(picUpload.ContentLength); } } aEntry.Heading = model.Entries.Heading; aEntry.text = model.Entries.text; aEntry.Date = DateTime.Now; aEntry.Author = user; user.EducationEntries.Add(aEntry); db.EntryEducation.Add(aEntry); db.SaveChanges(); if (model.SelectedTagIds != null) { foreach (var item in model.SelectedTagIds) { var selectedTag = new EducationTagEntries(); selectedTag.EntryId = db.EntryEducation.Max(x => x.Id); selectedTag.TagId = db.EducationTag.Where(x => x.TagName == item).SingleOrDefault().Id.ToString(); db.EducationTagEntries.Add(selectedTag); } } db.SaveChanges(); return(RedirectToAction("Education", "EntryInformative", new { id = user.Id })); } var tags = new List <SelectListItem>(); tags = db.EducationTag.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.TagName }).ToList(); model.TagNameList = tags; return(View(model)); }
public ActionResult EditEducation(CreateEducationViewModel model, HttpPostedFileBase picUpload) //Av någon anledning följer inte tagg-listan med... { EntryEducation entryToUpdate = new EntryEducation(); if (ModelState.IsValid) { entryToUpdate = db.EntryEducation.Find(model.Entries.Id); entryToUpdate.text = model.Entries.text; entryToUpdate.Heading = model.Entries.Heading; entryToUpdate.Date = model.Entries.Date; if (picUpload != null && picUpload.ContentLength > 0) { entryToUpdate.Filename = picUpload.FileName; entryToUpdate.ContentType = picUpload.ContentType; using (var reader = new BinaryReader(picUpload.InputStream)) { entryToUpdate.File = reader.ReadBytes(picUpload.ContentLength); } } db.SaveChanges(); var entryTags = db.EducationTagEntries.Where(x => x.EntryId == model.Entries.Id).ToList(); if (entryTags != null) { db.EducationTagEntries.RemoveRange(entryTags); db.SaveChanges(); } if (model.SelectedTagIds != null) { foreach (var item in model.SelectedTagIds) { var selectedTag = new EducationTagEntries(); selectedTag.EntryId = db.EntryEducation.Max(x => x.Id); try { selectedTag.TagId = db.EducationTag.SingleOrDefault(x => x.TagName == item).Id.ToString(); } catch { selectedTag.TagId = item; } db.EducationTagEntries.Add(selectedTag); } } db.SaveChanges(); return(RedirectToAction("Education")); } var tags = new List <SelectListItem>(); tags = db.EducationTag.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.TagName }).ToList(); model.TagNameList = tags; model.ExistingTagNameList = new List <SelectListItem>(); return(View(model)); }