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 "<h3>From [HttpPost]SearchIndex: "+searchString+"</h3>";
//}

    public ActionResult SearchIndex(string movieGenre,string searchString)
    {
        var GenreLst = new List<string>();
        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 =&gt; s.Title.Contains(searchString));
        }
 
        if (string.IsNullOrEmpty(movieGenre))
        {
            return View(movies);
        }
 
        else
        {
            return View(movies.Where(x =&gt; 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 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2015
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