Created
April 5, 2017 00:33
-
-
Save jwill9999/2c1f722aa26efbb1d2c8f5a4bc6416dc to your computer and use it in GitHub Desktop.
Filter search box c#
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
*******************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