排序,筛选和分页MVC [英] Sorting, Filtering and Paging MVC

查看:69
本文介绍了排序,筛选和分页MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我已经在本教程的帮助下完成了所有排序,过滤和分页的工作,这非常方便,因为我对本材料非常陌生.无论如何,我现在在尝试对一些具有多个主键的表进行排序和过滤时遇到了问题.

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.

我创建了var应用程序和var数据库,但是我认为,如果我有一种将它们组合在一起的方法,那么我的分页就不会有问题.因为我的返回视图将更加简洁.

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));
    }

我添加了var数据库,因为我需要在database_表以及应用程序表中搜索值.

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

索引:

@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.

谢谢!

为了更加清晰,我还找到了一种更好地解释自己的方法.

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

推荐答案

下面是一个可以帮助您解决问题的示例

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());
    }

排序示例

 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());
}

要显示的视图

<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>

这篇关于排序,筛选和分页MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆