我有这个数据库需要排序/过滤。我当前的代码显示没有错误,但它也没有任何错误。
ApplicationUsersController.cs
// GET: ApplicationUsers
public ActionResult Index(string sortOrder, string searchString)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.EmailSortParm = sortOrder == "Email" ? "email_desc" : "Email";
var users = from c in db.Users
select c;
if (!String.IsNullOrEmpty(searchString))
{
users = users.Where(c => c.Name.ToUpper().Contains(searchString.ToUpper())
|| c.Email.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
users = users.OrderByDescending(c => c.Name);
break;
case "Email":
users = users.OrderBy(c => c.Email);
break;
case "email_desc":
users = users.OrderByDescending(c => c.Email);
break;
default:
users = users.OrderBy(c => c.Name);
break;
}
return View(db.Users.ToList());
}
Index.cshtml
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
@Html.ActionLink("Email", "Index", new { sortOrder = ViewBag.EmailSortParm })
</th>
<th>
@Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
排序和过滤似乎都不起作用。排序时,地址栏更改为
/ ApplicationUsers?中将sortOrder = name_desc
但订单根本没有变化。也许有人能看出问题出在哪里?
编辑:
'var users = from c in db.Users select c;
'字母“c”错了吗?我怎么知道?
答案 0 :(得分:2)
变化:
return View(db.Users.ToList());
要:
return View(users);
: - )
答案 1 :(得分:1)
您需要将两个参数传递给动作Index
:
@Html.ActionLink("Email", "Index", new { sortOrder = ViewBag.EmailSortParm, searchString = null })