For Solution of above problem, we are creating one student controller and its index view. We want to display a Filter records(i.e. First Name,Last Name ) in index view. For this we create a search string text box in a view for searching FirstName,LastName in student records.
this is a code of Student controller Index Action:
publicActionResultIndex(string sortOrder, string searchString, string currentFilter) { ViewBag.NameSortParm=String.IsNullOrEmpty(sortOrder)?"Name":""; ViewBag.DateSortParm= sortOrder =="Date"?"Date_desc":"Date"; ViewBag.CurrentFilter = searchString; if(!String.IsNullOrEmpty(searchString)){
students = students.Where(s => s.FirstName.ToUpper().Contains(searchString.ToUpper())|| s.LastName.ToUpper().Contains(searchString.ToUpper()));} { students = students.Where(s => s.FirstName.ToUpper().Contains(searchString.ToUpper()) || s.LastName.ToUpper().Contains(searchString.ToUpper())); }var students =from s in db.Student // Student is a table i.e. containing records of student select s; switch(sortOrder) { case"Name": students = students.OrderByDescending(s => s.FirstName); break; case"Date": students = students.OrderBy(s => s.EnrollmentDate); break; case"Date_desc": students = students.OrderByDescending(s => s.EnrollmentDate); break; default: students = students.OrderBy(s => s.FirstName); break; } returnView(students.ToList()); }
Now. we have changed the index view of student. Code is as Follows:
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
Liked By
Write Answer
How to Filtering with the entity framework is an ASP.NET MVC Application
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
Anupam Mishra
05-Feb-2016Now. we have changed the index view of student. Code is as Follows: