public ActionResult DeleteEducationConfirmed(int id)
        {
            EntryEducation entries = db.EntryEducation.Find(id);

            db.EntryEducation.Remove(entries);
            db.SaveChanges();
            return(RedirectToAction("Education", new { Id = User.Identity.GetUserId() }));
        }
        public ActionResult EditEducation(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            EntryEducation old = db.EntryEducation.Find(id);

            EntryEducation entryEducation = new EntryEducation();

            entryEducation.Id          = old.Id;
            entryEducation.Heading     = old.Heading;
            entryEducation.text        = old.text;
            entryEducation.Date        = DateTime.Now;
            entryEducation.Filename    = old.Filename;
            entryEducation.ContentType = old.ContentType;
            entryEducation.File        = old.File;
            entryEducation.Author      = old.Author;


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



            var model        = new CreateEducationViewModel();
            var tags         = new List <SelectListItem>();
            var existingTags = new List <SelectListItem>();


            tags = db.EducationTag.Select(x => new SelectListItem {
                Value = x.Id.ToString(), Text = x.TagName
            }).ToList();

            var tagEntries = db.EducationTagEntries.Where(x => x.EntryId == entryEducation.Id).ToList();

            List <string> tagIDs = new List <string>();

            foreach (var tag in tagEntries)
            {
                existingTags.Add(tags.First(x => x.Value == tag.TagId));
                tags.Remove(tags.First(x => x.Value == tag.TagId));
            }


            model.TagNameList         = tags;
            model.ExistingTagNameList = existingTags;
            model.Entries             = entryEducation;

            return(View(model));
        }
        public ActionResult RemoveFileEducation(int id)
        {
            EntryEducation entry = db.EntryEducation.First(x => x.Id == id);

            entry.File        = null;
            entry.ContentType = null;
            entry.Filename    = null;
            db.SaveChanges();

            return(RedirectToAction("EditEducation", new { id = id }));
        }
        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 DeleteEducation(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            EntryEducation entries = db.EntryEducation.Find(id);

            if (entries == null)
            {
                return(HttpNotFound());
            }
            return(View(entries));
        }
        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));
        }