Created
March 26, 2018 09:23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 WebApplication1.Models; | |
namespace WebApplication1.Controllers | |
{ | |
public class MoviesController : Controller | |
{ | |
private MovieDBContext db = new MovieDBContext(); | |
// GET: Movies/Index?Searchstring={text} | |
public ActionResult Index(string searchString) | |
{ | |
var movies = (from m in db.Movies | |
select m); | |
if(!String.IsNullOrEmpty(searchString)) | |
{ | |
movies = movies.Where(s => s.Title.Contains(searchString)); | |
} | |
return View(movies); | |
} | |
// GET: Movies/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: Movies/Create | |
public ActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: Movies/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,ReleaseDate,Genre,Price")] Movie movie) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Movies.Add(movie); | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
return View(movie); | |
} | |
// GET: Movies/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: Movies/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,ReleaseDate,Genre,Price")] Movie movie) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Entry(movie).State = EntityState.Modified; | |
db.SaveChanges(); | |
return RedirectToAction("Index"); | |
} | |
return View(movie); | |
} | |
// GET: Movies/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: Movies/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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment