Exemplo n.º 1
0
        public ActionResult Skriv(FormellaInläggViewModell model, HttpPostedFileBase[] files)
        {
            var db = new InfoDbContext();

            // var list = db.Kategori.ToList();
            model.AllaKategorier = new SelectList(db.Kategori, "Id", "Namn", 1);

            if (model.text != null && model.titel != null)
            {
                var currentUser = User.Identity.GetUserId();
                var nyttInlägg  = new FormellaInlägg
                {
                    UserId      = currentUser,
                    Titel       = model.titel,
                    Text        = model.text,
                    TypAvInlägg = "Formell"
                };

                db.FormellaInläggen.Add(nyttInlägg);
                db.SaveChanges();

                var bloggId = nyttInlägg.Id;



                if (files != null)
                {
                    foreach (HttpPostedFileBase file in files)
                    {
                        if (file != null)
                        {
                            var    FilNamn = Path.GetFileName(file.FileName);
                            string path    = Path.Combine(Server.MapPath("~/Filer"), FilNamn);
                            file.SaveAs(path);
                            string FilenSparadSom = "/Filer/" + FilNamn;
                            db.Filer.Add(new Fil
                            {
                                BloggInläggId = bloggId,
                                FilUrl        = FilenSparadSom
                            });
                        }
                    }
                }

                db.BlogginläggsKategorier.Add(new BlogginläggsKategori {
                    BloggId    = bloggId,
                    KategoriId = model.ValdKategori
                });

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult VäljKategori(FormellaInläggViewModell model)
        {
            var db = new InfoDbContext();

            model.AllaKategorier = new SelectList(db.Kategori, "Id", "Namn", 1);
            var valdKat = model.ValdKategori;

            if (valdKat > 0)
            {
                return(RedirectToAction("FiltreraKategori", new { kategoriId = valdKat }));
            }
            return(View(model));
        }