Sorting, Filtering and Paging MVC

asp.net-mvc c# entity-framework

Question

So essentially I've done all my sorting, filtering and paging with the help of this tutorial, which has been very, very handy because I'm very new to this material. Anyways, I'm having issues now trying to sort and filter a few of my tables which have more than one primary key.

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

I created the var applications and var databases but I think if I had a way to combine them I wouldn't have issues with my paging. Because my return view would be more concise.

public ActionResult Index(string sortOrder, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "AppID_desc" : "";
        ViewBag.NameSortParm = sortOrder == "Name" ? "AppName_desc" : "Name";
        ViewBag.ID2SortParm = sortOrder == "ID" ? "DatabaseID_desc" : "ID";
        ViewBag.Name2SortParm = sortOrder == "Name2" ? "DatabaseName_desc" : "Name2";

        if (Request.HttpMethod != "GET")
        {
            page = 1;
        }
        ViewBag.CurrentFilter = searchString;


        var applications = from a in db.Application_
                      select a;
        var databases = from d in db.Database_ //this is what I added
                        select d;
        if (!String.IsNullOrEmpty(searchString))
        {
            applications = applications.Where(s => s.AppName.ToUpper().Contains(searchString.ToUpper()));
            databases = databases.Where(d => d.DatabaseName.ToUpper().Contains(searchString.ToUpper())); //also what I added
        }
        switch (sortOrder)
        {
            case "AppID_desc":
                applications = applications.OrderByDescending(a => a.AppID);
                break;
            case "Name":
                applications = applications.OrderBy(a => a.AppName);
                break;
            case "AppName_desc":
                applications = applications.OrderByDescending(a => a.AppName);
                break;
            case "Name2":
                databases = databases.OrderBy(d=> d.DatabaseName);
                break;
            case "DatabaseName_desc":
                databases = databases.OrderByDescending(d => d.DatabaseName);
                break;
            default:
                applications = applications.OrderBy(a => a.AppID);
                break;

        }
        int pageSize = 10;
        int pageNumber = (page ?? 1);
        return View(applications.ToPagedList(pageNumber, pageSize));
    }

I added the var database because I need to search for values in the database_ table along with the application table.

The index:

@using (Html.BeginForm())
{
<p>
    Search Name: @Html.TextBox("Search_Data", ViewBag.FilterValue as string)
    <input type="submit" value="Find" />
</p>
}
<table class="table">

<tr>
    <th>
        @Html.ActionLink("AppID", "Index", new { sortOrder = ViewBag.IDSortParm })
    </th>
    <th>
        @Html.ActionLink("ApplicationName", "Index", new { sortOrder = ViewBag.NameSortParm })
    </th>
    <th>
        @Html.ActionLink("DatabaseID", "Index", new { sortOrder = ViewBag.ADSortParm })
    </th>
    <th>
        @Html.ActionLink("DatabaseName", "Index", new { sortOrder = ViewBag.Name2SortParm })
    </th>

I believe I'm having an issue with the index, but obviously I'm pretty clueless in general so whatever assistance you can offer would be greatly appreciated.

Thanks!!

EDIT: For more clarity, plus I found a way to explain myself better.

1
2
11/5/2014 4:21:14 PM

Popular Answer

Here is an example that should help you with your problem

 public ActionResult Index(string sortOrder)
    {
       ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
       ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";
       var students = from s in db.Students
                      select s;
       switch (sortOrder)
       {
          case "Name_desc":
             students = students.OrderByDescending(s => s.LastName);
             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.LastName);
             break;
       }
       return View(students.ToList());
    }

Example of sort

 ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

Here is an example of a view to display

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>First Name
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {

I am also posting a search deature whigh might be handy

public ViewResult Index(string sortOrder, string searchString)
{
    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
    var students = from s in db.Students
                   select s;
    if (!String.IsNullOrEmpty(searchString))
    {
        students = students.Where(s => s.LastName.Contains(searchString)
                               || s.FirstMidName.Contains(searchString));
    }
    switch (sortOrder)
    {
        case "name_desc":
            students = students.OrderByDescending(s => s.LastName);
            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.LastName);
            break;
    }

    return View(students.ToList());
}

the view to display

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")  
        <input type="submit" value="Search" /></p>
}

<table>
    <tr>
2
1/14/2015 3:12:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow