예제 #1
0
        public ActionResult Edit([Bind(Include = "Id,Titulo,Sinopse,AnoLanc,LinkTrailer,ImagensFilme,RealizadorId")]
                                 Filme filme, HttpPostedFileBase ImagemFilme, List <int> Categorias, List <int> Atores)
        {
            if (Atores == null)
            {
                Atores = new List <int>();
            }
            else if (Categorias == null)
            {
                Categorias = new List <int>();
            }
            else
            if (ModelState.IsValid)
            {
                //lista das categorias dos filmes para Editar
                IQueryable <Categorias> temp2 = db.Categorias.Where(a => Categorias.Any(aa => a.Id == aa));
                filme.Categorias = temp2.ToList();

                //lista dos atores para editar
                IQueryable <Ator> temp3 = db.Ators.Where(a => Atores.Any(aa => a.Id == aa));
                filme.Atores          = temp3.ToList();
                db.Entry(filme).State = EntityState.Modified;

                //Editar Imagem
                if (ImagemFilme != null)
                {
                    if (System.IO.File.Exists(Server.MapPath("~/Imagens/" + filme.Id + filme.ImagensFilme)))
                    {
                        System.IO.File.Delete(Server.MapPath("~/Imagens/" + filme.Id + filme.ImagensFilme));
                    }
                    filme.ImagensFilme = Path.GetExtension(ImagemFilme.FileName);
                    ImagemFilme.SaveAs(Path.Combine(Server.MapPath("~/Imagens/" + filme.Id + filme.ImagensFilme)));
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.RealizadorId   = new SelectList(db.Realizador, "Id", "NomeRealizador", filme.RealizadorId);
            ViewBag.Categorias     = new SelectList(db.Categorias, "Id", "Nome");
            ViewBag.sel_Categorias = Categorias.ToList();
            ViewBag.Atores         = new SelectList(db.Ators, "Id", "Nome");
            ViewBag.sel_Atores     = Atores.ToList();
            return(View(filme));
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "Id,Titulo,Sinopse,AnoLanc,LinkTrailer,ImagensFilme,RealizadorId")]
                                   Filme filme, HttpPostedFileBase ImagensFilme, List <int> Categorias, List <int> Atores)
        {
            if (ImagensFilme != null)
            {
                filme.ImagensFilme = Path.GetExtension(ImagensFilme.FileName);
            }

            if (Atores == null)
            {
                Atores = new List <int>();
            }
            else if (Categorias == null)
            {
                Categorias = new List <int>();
            }
            else
            if (ModelState.IsValid)
            {
                db.Filme.Add(filme);
                //Para adicionar Lista de categorias aos filmes
                IQueryable <Categorias> temp2 = db.Categorias.Where(a => Categorias.Any(aa => a.Id == aa));
                filme.Categorias = temp2.ToList();

                //Para adicionar lista dos atores
                IQueryable <Ator> temp3 = db.Ators.Where(a => Atores.Any(aa => a.Id == aa));
                filme.Atores = temp3.ToList();
                db.SaveChanges();

                //Para adiciona uma imagem ao Filme
                ImagensFilme.SaveAs(Path.Combine(Server.MapPath("~/Imagens/" + filme.Id + filme.ImagensFilme)));
                return(RedirectToAction("Index"));
            }
            ViewBag.sel_Categorias = Categorias.ToList();
            ViewBag.sel_Atores     = Atores.ToList();
            ViewBag.Categorias     = new SelectList(db.Categorias, "Id", "Nome");
            ViewBag.Atores         = new SelectList(db.Ators, "Id", "Nome");
            ViewBag.RealizadorId   = new SelectList(db.Realizador, "Id", "NomeRealizador", filme.RealizadorId);
            return(View(filme));
        }
 public List <Categoria> obtenerCategorias() => Categorias.ToList();