Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created April 5, 2017 00:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwill9999/2c1f722aa26efbb1d2c8f5a4bc6416dc to your computer and use it in GitHub Desktop.
Save jwill9999/2c1f722aa26efbb1d2c8f5a4bc6416dc to your computer and use it in GitHub Desktop.
Filter search box c#
*******************Example of search by title and genre ********************
public class MovieController : Controller
{
private MovieDBContext db = new MovieDBContext();
public ActionResult Index(string movieGenre, string searchString)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.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))
{
movies = movies.Where(x => x.Genre == movieGenre);
}
return View(movies);
}
}
****************htlm markup************
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm())
{
<p>
Title: @Html.TextBox("searchString")
Genre: @Html.DropDownList("movieGenre", "All")
<br />
<br />
<input type="submit" value="Filter" />
</p>
}
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment