Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MovieController
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using ou3Movie.Models;
- using PagedList;
- namespace ou3Movie.Controllers
- {
- public class MovieController : Controller
- {
- private MovieContext db = new MovieContext();
- // GET: Movie
- public ViewResult Index(string sortOrder, string currentFilter, string search, int? page)
- {
- ViewBag.TitleSort = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
- ViewBag.DirectorSort = sortOrder == "Director" ? "director_desc" : "Director";
- ViewBag.GenreSort = sortOrder == "Genre" ? "genre_desc" : "Genre";
- if (search != null)
- {
- page = 1;
- }
- else
- {
- search = currentFilter;
- }
- ViewBag.CurrentFilter = search;
- var Movies = from s in db.Movies
- select s;
- if (!String.IsNullOrEmpty(search))
- {
- Movies = Movies.Where(s => s.Title.Contains(search) || s.Director.Contains(search) || s.Genre.Contains(search));
- }
- switch (sortOrder)
- {
- case "title_desc":
- Movies = Movies.OrderByDescending(s => s.Title);
- break;
- case "Director":
- Movies = Movies.OrderBy(s => s.Director);
- break;
- case "director_desc":
- Movies = Movies.OrderByDescending(s => s.Director);
- break;
- case "Genre":
- Movies = Movies.OrderBy(s => s.Genre);
- break;
- case "genre_desc":
- Movies = Movies.OrderByDescending(s => s.Genre);
- break;
- default:
- Movies = Movies.OrderBy(s => s.Title);
- break;
- }
- int pageSize = 15;
- int pageNumber = (page ?? 1);
- return View(Movies.ToPagedList(pageNumber, pageSize));
- }
- // GET: Movie/Details/5
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Movie movie = db.Movies.Find(id);
- if (movie == null)
- {
- return HttpNotFound();
- }
- return View(movie);
- }
- // GET: Movie/Create
- public ActionResult Create()
- {
- return View();
- }
- // POST: Movie/Create
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create([Bind(Include = "ID,Title,Director,Genre")] Movie movie)
- {
- if (ModelState.IsValid)
- {
- db.Movies.Add(movie);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(movie);
- }
- // GET: Movie/Edit/5
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Movie movie = db.Movies.Find(id);
- if (movie == null)
- {
- return HttpNotFound();
- }
- return View(movie);
- }
- // POST: Movie/Edit/5
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include = "ID,Title,Director,Genre")] Movie movie)
- {
- if (ModelState.IsValid)
- {
- db.Entry(movie).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(movie);
- }
- // GET: Movie/Delete/5
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Movie movie = db.Movies.Find(id);
- if (movie == null)
- {
- return HttpNotFound();
- }
- return View(movie);
- }
- // POST: Movie/Delete/5
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(int id)
- {
- Movie movie = db.Movies.Find(id);
- db.Movies.Remove(movie);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement