Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. MovieController
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Entity;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. using ou3Movie.Models;
  12. using PagedList;
  13.  
  14. namespace ou3Movie.Controllers
  15. {
  16. public class MovieController : Controller
  17. {
  18. private MovieContext db = new MovieContext();
  19.  
  20. // GET: Movie
  21. public ViewResult Index(string sortOrder, string currentFilter, string search, int? page)
  22. {
  23.  
  24. ViewBag.TitleSort = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
  25. ViewBag.DirectorSort = sortOrder == "Director" ? "director_desc" : "Director";
  26. ViewBag.GenreSort = sortOrder == "Genre" ? "genre_desc" : "Genre";
  27.  
  28. if (search != null)
  29. {
  30. page = 1;
  31. }
  32. else
  33. {
  34. search = currentFilter;
  35. }
  36.  
  37. ViewBag.CurrentFilter = search;
  38.  
  39. var Movies = from s in db.Movies
  40. select s;
  41.  
  42.  
  43. if (!String.IsNullOrEmpty(search))
  44. {
  45. Movies = Movies.Where(s => s.Title.Contains(search) || s.Director.Contains(search) || s.Genre.Contains(search));
  46. }
  47.  
  48. switch (sortOrder)
  49. {
  50.  
  51.  
  52. case "title_desc":
  53. Movies = Movies.OrderByDescending(s => s.Title);
  54. break;
  55.  
  56. case "Director":
  57. Movies = Movies.OrderBy(s => s.Director);
  58. break;
  59. case "director_desc":
  60. Movies = Movies.OrderByDescending(s => s.Director);
  61. break;
  62.  
  63. case "Genre":
  64. Movies = Movies.OrderBy(s => s.Genre);
  65. break;
  66.  
  67. case "genre_desc":
  68. Movies = Movies.OrderByDescending(s => s.Genre);
  69. break;
  70.  
  71. default:
  72. Movies = Movies.OrderBy(s => s.Title);
  73. break;
  74. }
  75.  
  76. int pageSize = 15;
  77. int pageNumber = (page ?? 1);
  78.  
  79.  
  80. return View(Movies.ToPagedList(pageNumber, pageSize));
  81. }
  82.  
  83. // GET: Movie/Details/5
  84. public ActionResult Details(int? id)
  85. {
  86. if (id == null)
  87. {
  88. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  89. }
  90. Movie movie = db.Movies.Find(id);
  91. if (movie == null)
  92. {
  93. return HttpNotFound();
  94. }
  95. return View(movie);
  96. }
  97.  
  98. // GET: Movie/Create
  99. public ActionResult Create()
  100. {
  101. return View();
  102. }
  103.  
  104. // POST: Movie/Create
  105. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  106. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  107. [HttpPost]
  108. [ValidateAntiForgeryToken]
  109. public ActionResult Create([Bind(Include = "ID,Title,Director,Genre")] Movie movie)
  110. {
  111. if (ModelState.IsValid)
  112. {
  113. db.Movies.Add(movie);
  114. db.SaveChanges();
  115. return RedirectToAction("Index");
  116. }
  117.  
  118. return View(movie);
  119. }
  120.  
  121. // GET: Movie/Edit/5
  122. public ActionResult Edit(int? id)
  123. {
  124. if (id == null)
  125. {
  126. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  127. }
  128. Movie movie = db.Movies.Find(id);
  129. if (movie == null)
  130. {
  131. return HttpNotFound();
  132. }
  133. return View(movie);
  134. }
  135.  
  136. // POST: Movie/Edit/5
  137. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  138. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  139. [HttpPost]
  140. [ValidateAntiForgeryToken]
  141. public ActionResult Edit([Bind(Include = "ID,Title,Director,Genre")] Movie movie)
  142. {
  143. if (ModelState.IsValid)
  144. {
  145. db.Entry(movie).State = EntityState.Modified;
  146. db.SaveChanges();
  147. return RedirectToAction("Index");
  148. }
  149. return View(movie);
  150. }
  151.  
  152. // GET: Movie/Delete/5
  153. public ActionResult Delete(int? id)
  154. {
  155. if (id == null)
  156. {
  157. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  158. }
  159. Movie movie = db.Movies.Find(id);
  160. if (movie == null)
  161. {
  162. return HttpNotFound();
  163. }
  164. return View(movie);
  165. }
  166.  
  167. // POST: Movie/Delete/5
  168. [HttpPost, ActionName("Delete")]
  169. [ValidateAntiForgeryToken]
  170. public ActionResult DeleteConfirmed(int id)
  171. {
  172. Movie movie = db.Movies.Find(id);
  173. db.Movies.Remove(movie);
  174. db.SaveChanges();
  175. return RedirectToAction("Index");
  176. }
  177.  
  178. protected override void Dispose(bool disposing)
  179. {
  180. if (disposing)
  181. {
  182. db.Dispose();
  183. }
  184. base.Dispose(disposing);
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement