コード例 #1
0
ファイル: MovieController.cs プロジェクト: ztronk/MovieApp
        public ActionResult Create(MovieEnvelop movie)
        {
            try
            {
                if (CheckUser())
                {
                    return(RedirectToAction("Index", "Movie"));
                }

                if (ModelState.IsValid)
                {
                    if (movie.Picture != null)
                    {
                        string fileName = movie.Id.ToString();
                        string extFile  = Path.GetExtension(movie.Picture.FileName);
                        movie.Poster = string.Format("{0}{1}", fileName, extFile);
                        movie.Picture.SaveAs(Server.MapPath($"~/Files/{fileName}{extFile}"));
                    }

                    movie.Id          = Guid.NewGuid();
                    movie.CreatedDate = DateTime.Now;
                    movie.UserId      = User.Identity.GetUserId();
                    movieContext.Create(movie.Translate());
                    return(RedirectToAction("Index", "Movie"));
                }
                else
                {
                    return(View(movie));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index", "Movie"));
            }
        }
コード例 #2
0
ファイル: MovieController.cs プロジェクト: ztronk/MovieApp
        public ActionResult Edit(MovieEnvelop movieEnvelop)
        {
            try
            {
                if (CheckUser())
                {
                    return(RedirectToAction("Index", "Movie"));
                }

                if (ModelState.IsValid)
                {
                    if (movieEnvelop.Picture != null)
                    {
                        string fileName = movieEnvelop.Id.ToString();
                        string extFile  = Path.GetExtension(movieEnvelop.Picture.FileName);

                        movieEnvelop.Poster = string.IsNullOrEmpty(movieEnvelop.Poster)
                                        ? string.Format("{0}{1}", fileName, extFile)
                                        : movieEnvelop.Poster;

                        movieEnvelop.Picture.SaveAs(Server.MapPath($"~/Files/{movieEnvelop.Poster}"));

                        var movie = movieContext.Get(movieEnvelop.Id);
                        movie.Poster = movieEnvelop.Poster;

                        movieContext.Update(movie);
                    }
                    return(RedirectToAction("Index", "Movie"));
                }
                else
                {
                    return(View(movieEnvelop));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index", "Movie"));
            }
        }