public ActionResult Edit(int id) { var vinyl = context.Vinyls.SingleOrDefault(v => v.Id == id); var genre = context.Genres.ToList(); var label = context.Labels.ToList(); if (vinyl == null) { return(HttpNotFound()); } var vinylModel = new VinylFormViewModel() { Id = (int)vinyl.Id, Title = vinyl.Title, Artist = vinyl.Artist, Genre = (byte)vinyl.GenreId, ReleaseYear = vinyl.ReleaseYear, Label = (byte)vinyl.LabelId, Price = vinyl.Price, Genres = genre, Labels = label }; return(View("VinylForm", vinylModel)); }
public ActionResult Create() { var viewModel = new VinylFormViewModel() { Genres = context.Genres.ToList(), Labels = context.Labels.ToList() }; return(View(viewModel)); }
public ActionResult Save(Vinyl vinyl, HttpPostedFileBase postedImage, HttpPostedFileBase postedAudio) { // Validation: If the model is not valid maintain the specific view if (!ModelState.IsValid) { var viewModel = new VinylFormViewModel() { Vinyl = vinyl, Categories = context.Categories.ToList(), Genres = context.Genres.ToList(), RecordLabels = context.RecordLabels.ToList(), //ContentFiles = context.ContentFiles.ToList() }; return(View("VinylForm", viewModel)); } // Create if (vinyl.Id == 0) { context.Vinyls.Add(vinyl); } // Edit else { var vinylInDb = context.Vinyls.Single(v => v.Id == vinyl.Id); // properties from base model Product vinylInDb.Price = vinyl.Price; vinylInDb.Quantity = vinyl.Quantity; vinylInDb.IsAvailable = vinyl.IsAvailable; //vinylInDb.CategoryId = vinyl.CategoryId; // categories are constants //vinylInDb.ContentFiles = vinyl.ContentFiles; //vinylInDb.OrderDetails = vinyl.OrderDetails; // properties from derived model Vinyl //vinylInDb.Artist = vinyl.Artist; //vinylInDb.Title = vinyl.Title; //vinylInDb.ReleaseDate = vinyl.ReleaseDate; //vinylInDb.GenreId = vinyl.GenreId; //vinylInDb.RecordLabelId = vinyl.RecordLabelId; //vinylInDb.Formats = vinyl.Formats; } context.SaveChanges(); return(RedirectToAction("Index", "Vinyls")); }
// GET: //[Authorize] public ViewResult New() { var categories = context.Categories.ToList(); var genres = context.Genres.ToList(); var recordLabels = context.RecordLabels.ToList(); //var contentFiles = context.ContentFiles.ToList(); var viewModel = new VinylFormViewModel() { Vinyl = new Vinyl(), Categories = categories, Genres = genres, RecordLabels = recordLabels //ContentFiles = contentFiles }; return(View("VinylForm", viewModel)); }
public ActionResult Save(VinylFormViewModel vinylModel) { if (!ModelState.IsValid) { var musicModel = new VinylFormViewModel() { Genres = context.Genres.ToList(), Labels = context.Labels.ToList() }; return(View("VinylForm", musicModel)); } if (vinylModel.Id == 0) // Create { var vinyl = new Vinyl() { Id = vinylModel.Id, Title = vinylModel.Title, Artist = vinylModel.Artist, ReleaseYear = vinylModel.ReleaseYear, Price = vinylModel.Price, GenreId = (int)vinylModel.Genre, LabelId = (int)vinylModel.Label }; context.Vinyls.Add(vinyl); } else //Edit { var vinylDb = context.Vinyls.SingleOrDefault(v => v.Id == vinylModel.Id); // autes oi tesseris grammes mporeis na peis oti einai i whiteList vinylDb.Title = vinylModel.Title; vinylDb.Artist = vinylModel.Artist; vinylDb.ReleaseYear = vinylModel.ReleaseYear; vinylDb.GenreId = vinylModel.Genre; vinylDb.LabelId = vinylModel.Label; vinylDb.Price = vinylModel.Price; } context.SaveChanges(); return(RedirectToAction("Index", "Vinyls")); }
// GET public ActionResult Edit(int id) { var vinyl = context.Vinyls.SingleOrDefault(v => v.Id == id); // Bad scenario if (vinyl == null) { return(HttpNotFound()); } // Good scenario var viewModel = new VinylFormViewModel() { Vinyl = vinyl, Categories = context.Categories.ToList(), Genres = context.Genres.ToList(), RecordLabels = context.RecordLabels.ToList() //ContentFiles = context.ContentFiles.ToList(), }; return(View("VinylForm", viewModel)); }