基本上,我已经在本教程的帮助下完成了所有的排序,过滤和分页,这非常非常方便,因为我对这些材料非常陌生。无论如何,我现在遇到的问题是尝试对包含多个主键的一些表进行排序和过滤。
我创建了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数据库,因为我需要在database_表中搜索值以及应用程序表。
索引:
@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>
我相信我对索引有一个问题,但显然我一般都很无能,所以无论你提供什么样的帮助都会非常感激。
谢谢!
编辑: 为了更清晰,我发现了一种更好地解释自己的方法。
答案 0 :(得分:2)
以下是一个可以帮助您解决问题的示例
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) {
我也发布了一个搜索deature whigh可能很方便
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>
答案 1 :(得分:1)
我想出了我遇到的排序问题。
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;
这是我的错误。
我也想知道我将如何解决我的搜索问题。我刚刚将AppID中的int转换为字符串。
if (!String.IsNullOrEmpty(searchString))
{
appdb = appdb.Where(a => a.AppID.ToString().Contains(searchString.ToUpper()));
}
答案 2 :(得分:0)
您正在搜索的Database
数据可能会以某种方式与Application
数据相关联。我要走出去,说每个Application
可能有一个Database
。您可能希望使用导航属性来修改applications
查询,而不是使用单独的变量,如下所示:
case "Name2":
applications = applications.OrderBy(a => a.Database.DatabaseName);
break;
case "DatabaseName_desc":
applications = applications.OrderByDescending(a => a.Database.DatabaseName);
break;