How to Filtering with the entity framework is an ASP.NET MVC Application

Total Post:135

 741  View(s)
Rate this:
Hi Everyone,

I want to know how to Filter record with the using of entity framework in ASP.NET MVC. Can anyone give me a solution.

Thank you.
  1. Post:135

    Re: How to Filtering with the entity framework is an ASP.NET MVC Application

    Finally, I have resolve this problem.
    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:
    public ActionResult Index(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);
    case "Date":
    students = students.OrderBy(s => s.EnrollmentDate);
    case "Date_desc":
    students = students.OrderByDescending(s => s.EnrollmentDate);
    students = students.OrderBy(s => s.FirstName);
    return View(students.ToList());
    Now. we have changed the index view of student. Code is as Follows: 

    <h2>Student Index</h2>


    @Html.ActionLink("Create New", "Create")

    @using (Html.BeginForm("Index", "Student", FormMethod.Get))



    Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)

    <input type="submit" value="Search" />

      Modified On Apr-11-2018 01:24:48 AM



Enter your email address here always to be updated. We promise not to spam!