public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album)
        {
            if (ModelState.IsValid)
            {
                if (Request != null)
                {
                    // upload album cover if there is one
                    if (Request.Files.Count > 0)
                    {
                        var file = Request.Files[0];

                        if (file.FileName != null && file.ContentLength > 0)
                        {
                            // get file path dynamically
                            string path = Server.MapPath("~/Content/Images/") + file.FileName;
                            file.SaveAs(path);
                            album.AlbumArtUrl = "/Content/Images/" + file.FileName;
                        }
                    }
                }

                db.Save(album);
                //db.Albums.Add(album);
                //db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId  = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);

            return(View("Create", album));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album)
        {
            if (ModelState.IsValid)
            {
                //db.Albums.Add(album);
                //db.SaveChanges();
                db.Save(album);
                return(RedirectToAction("Index"));
            }

            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId  = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return(View("Create", album));
        }