Click here to Skip to main content
See more: MVC
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Models;
 
namespace MvcMovie.Controllers
{
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
 
//
// GET: /Movies/
 
public ViewResult Index()
{
return View(db.Movies.ToList());
}
 
//
// GET: /Movies/Details/5
 
public ViewResult Details(int id)
{
Movie movie = db.Movies.Find(id);
return View(movie);
}
 
//
// GET: /Movies/Create
 
public ActionResult Create()
{
return View();
}
 
//
// POST: /Movies/Create
 
[HttpPost]
public ActionResult Create(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=0)
{
Movie movie = db.Movies.Find(id);
if (movie==null)
{
return HttpNotFound();
}
return View(movie);
}
 
//
// POST: /Movies/Edit/5
 
[HttpPost]
public ActionResult Edit(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)
{
Movie movie = db.Movies.Find(id);
return View(movie);
}
 
//
// POST: /Movies/Delete/5
 
[HttpPost, ActionName("Delete")]
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)
{
db.Dispose();
base.Dispose(disposing);
}
 

// public ActionResult SearchIndex(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);
// }
 
//[HttpPost]
// public string SearchIndex(FormCollection fc,string searchString)
//{
// return "

From [HttpPost]SearchIndex: "+searchString+"

";
//}
 

public ActionResult SearchIndex(string movieGenre,string searchString)
{
var GenreLst = new List();
var GenryQuery = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenryQuery.Distinct());
ViewBag.movieGenre=new SelectList(GenreLst);




var movies = from m in db.Movies select m;


if(!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
 
if (string.IsNullOrEmpty(movieGenre))
{
return View(movies);
}
 
else
{
return View(movies.Where(x => x.Genre == movieGenre));
}
}
}
}
Closed because This post is not a question, or has not been phrased in a way that allows a reasonable answer to be provided. Reported by Richard Deeming, SoMad, Zoltán Zörgő, __TR__ on Friday, December 12, 2014 3:28am.
Posted 30-Mar-13 19:35pm
Edited 12-Dec-14 3:28am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100