Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 排序、过滤和分页MVC_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 排序、过滤和分页MVC

C# 排序、过滤和分页MVC,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,基本上,我在本教程的帮助下完成了所有的排序、过滤和分页,这非常非常方便,因为我对本教程非常陌生。不管怎么说,我现在在尝试对几个包含多个主键的表进行排序和筛选时遇到了问题 我创建了var应用程序和var数据库,但我认为如果我有办法将它们结合起来,我的分页就不会有问题。因为我的返回视图会更简洁 public ActionResult Index(string sortOrder, string searchString, int? page) { ViewBag.Curre

基本上,我在本教程的帮助下完成了所有的排序、过滤和分页,这非常非常方便,因为我对本教程非常陌生。不管怎么说,我现在在尝试对几个包含多个主键的表进行排序和筛选时遇到了问题

我创建了var应用程序和var数据库,但我认为如果我有办法将它们结合起来,我的分页就不会有问题。因为我的返回视图会更简洁

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数据库,因为我需要在数据库表和应用程序表中搜索值

索引:

@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>
@使用(Html.BeginForm())
{

搜索名称:@Html.TextBox(“搜索数据”,ViewBag.FilterValue为字符串)

} @ActionLink(“AppID”,“Index”,new{sortOrder=ViewBag.IDSortParm}) @ActionLink(“ApplicationName”,“Index”,new{sortOrder=ViewBag.NameSortParm}) @ActionLink(“DatabaseID”,“Index”,new{sortOrder=ViewBag.ADSortParm}) @ActionLink(“DatabaseName”,“Index”,new{sortOrder=ViewBag.Name2SortParm})
我相信我对索引有问题,但很明显,我一般都很无知,所以无论你能提供什么帮助,我都将不胜感激

谢谢

编辑:
为了更清楚,我还找到了一种更好地解释自己的方法。

您正在搜索的
数据库
数据可能与
应用程序
数据有某种关联。我要冒险说,每个
应用程序
可能都有一个
数据库
。您可能希望使用导航属性来修改
应用程序
查询,而不是使用单独的变量,如下所示:

        case "Name2":
            applications = applications.OrderBy(a => a.Database.DatabaseName);
            break;
        case "DatabaseName_desc":
            applications = applications.OrderByDescending(a => a.Database.DatabaseName);
            break;

我解决了我遇到的排序问题

   var applications = from a in db.Application_
                  select a;
   var databases = from d in db.Database_ //this is what I added
                    select d;
需要:

   var appdb = from a in db.AppDB_
                      select a;
这是我的一个错误

我还刚刚想出了如何解决我的搜索问题。我刚刚将int从AppID转换为字符串

  if (!String.IsNullOrEmpty(searchString))
  {
     appdb = appdb.Where(a => a.AppID.ToString().Contains(searchString.ToUpper()));
  }      

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

 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";
下面是要显示的视图的示例

<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) {
<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>
要显示的视图

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

@ActionLink(“新建”、“创建”)

@使用(Html.BeginForm()) { 按名称查找:@Html.TextBox(“搜索字符串”)

}
“我现在在尝试对一些表进行排序和筛选时遇到问题”-问题是什么(它不排序,有时不排序,只对某些页面排序,不筛选,等等)?您的问题目前不清楚实际问题是什么(预期行为,当前行为)。具有多个主键的表根本不会排序。据我所知,您试图在一个网格中同时查看表应用程序和数据库,并能够单击标题对数据进行排序?是,如果我的胡言乱语很难理解,我很抱歉。这东西真的很难说。请为表、应用程序和数据库提供实体定义。它似乎不起作用。它不认识d。在第一个上下文中,然后是数据库。在第二个上下文中,我假设
d
是印刷错误。将其更改为
a
它无法识别数据库。现在。我想这是因为我的var是如何声明的。var应用程序=从db.Application中的a选择a@MomoFeerce:您的实体上有导航属性吗?他们之间的关系怎么样?