Beispiel #1
0
        public ActionResult Create()
        {
            var editorViewModel = new AlbumEditorViewModel
            {
                ArtistSelectList=new SelectList(db.Artists,"Id","Name"),
                AlbumCatetorySelectList=new SelectList(db.Genres,"Id","Name")
            };

            return View(editorViewModel);
        }
Beispiel #2
0
        public ActionResult Create(AlbumEditorViewModel editorViewModel, HttpPostedFileBase albumCover)
        {
            if (albumCover != null && !isPicture(albumCover.FileName))
            {
                ModelState.AddModelError("albumCover", "您上傳的檔案格式有誤,只能上傳圖檔");
            }

            if (ModelState.IsValid)
            {
                Album album = editorViewModel.Album;
                db.Albums.Add(album);
                db.SaveChanges();
                SaveAlbumCover(album.Id, albumCover);
                //用來讓view存取新增成功的唱片資訊
                TempData["createdId"] = album.Id;
                TempData["createdName"] =album.Name;
                return RedirectToAction("Album","Manage");
            }

            editorViewModel.ArtistSelectList = new SelectList(db.Artists, "Id", "Name");
            editorViewModel.AlbumCatetorySelectList = new SelectList(db.Genres, "Id", "Name");
            return View(editorViewModel);
        }
Beispiel #3
0
        public ActionResult Edit(AlbumEditorViewModel editorViewModel, HttpPostedFileBase albumCover)
        {
            if (albumCover!=null && !isPicture(albumCover.FileName))
            {
                ModelState.AddModelError("albumCover", "您上傳的檔案格式有誤,只能上傳圖檔");
            }
            Album newAlbum=editorViewModel.Album;
            Album oldAlbum = db.Albums.FirstOrDefault(alb => alb.Id == newAlbum.Id);

            if (oldAlbum !=null && ModelState.IsValid)
            {
                oldAlbum.Name = newAlbum.Name;
                oldAlbum.Description = newAlbum.Description;
                oldAlbum.PublicDate = newAlbum.PublicDate;
                oldAlbum.Price = newAlbum.Price;
                oldAlbum.Stock = newAlbum.Stock;
                oldAlbum.ArtistId = newAlbum.ArtistId;
                oldAlbum.GenreId = newAlbum.GenreId;
                UpdateSongs(oldAlbum, newAlbum.Songs);

                db.SaveChanges();
                SaveAlbumCover(oldAlbum.Id, albumCover);
                //用來讓view存取更新成功的唱片資訊
                TempData["success"] = true;
                return RedirectToAction("Edit", new { id = oldAlbum.Id });
            }

            return View(editorViewModel);
        }
Beispiel #4
0
        public ActionResult Edit(int id)
        {
            Album album = db.Albums.FirstOrDefault(alb => alb.Id == id);

            if (album == null) return HttpNotFound();
            var createViewModel = new AlbumEditorViewModel
            {
                Album=album,
                ArtistSelectList = new SelectList(db.Artists, "Id", "Name",album.Artist.Id),
                AlbumCatetorySelectList = new SelectList(db.Genres, "Id", "Name",album.Genre.Id)
            };

            return View(createViewModel);
        }