예제 #1
0
        public ActionResult AddAudiobook(EditAudiobookViewModel model, HttpPostedFileBase file)
        {
            if (model.Audiobook.AudiobookId > 0)
            {
                // modyfikacja Audiobook
                db.Entry(model.Audiobook).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("AddAudiobook", new { confirmation = true }));
            }
            else
            {
                // Sprawdzenie, czy użytkownik wybrał plik
                if (file != null && file.ContentLength > 0)
                {
                    if (ModelState.IsValid)
                    {
                        // Generowanie pliku
                        var fileExt  = Path.GetExtension(file.FileName);
                        var filename = Guid.NewGuid() + fileExt;

                        var path = Path.Combine(Server.MapPath(AppConfig.ImagesFolderRelative), filename);
                        file.SaveAs(path);

                        model.Audiobook.ImageFileName = filename;
                        model.Audiobook.DateAdded     = DateTime.Now;

                        db.Entry(model.Audiobook).State = EntityState.Added;
                        db.SaveChanges();

                        return(RedirectToAction("AddAudiobook", new { confirmation = true }));
                    }
                    else
                    {
                        var categories = db.Categories.ToList();
                        model.Categories = categories;
                        return(View(model));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Nie wskazano pliku");
                    var categories = db.Categories.ToList();
                    model.Categories = categories;
                    return(View(model));
                }
            }
        }
예제 #2
0
        public ActionResult AddAudiobook(int?AudiobookId, bool?confirmation)
        {
            Audiobook audiobook;

            if (AudiobookId.HasValue)
            {
                ViewBag.EditMode = true;
                audiobook        = db.Audiobooks.Find(AudiobookId);
            }
            else
            {
                ViewBag.EditMode = false;
                audiobook        = new Audiobook();
            }

            var result = new EditAudiobookViewModel();

            result.Categories   = db.Categories.ToList();
            result.Audiobook    = audiobook;
            result.Confirmation = confirmation;

            return(View(result));
        }