【发布时间】:2016-01-09 18:30:24
【问题描述】:
我一直在尝试对一个项目进行排序,但遇到了困难。我已经查看了有关如何完成此操作的各种参考资料,但它仍然无法在我的项目中使用。我看过了
和
http://www.c-sharpcorner.com/UploadFile/4b0136/perform-paging-searching-sorting-in-Asp-Net-mvc-5/
我拥有的是我的控制器
public ActionResult Index(string sortBy, string searchString)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortBy) ? "Surname desc" : "";
ViewBag.DateSort = sortBy == "StartDate" ? "date desc" : "StartDate";
var students = from s in db.Students
select s;
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.Surname.Contains(searchString));
}
switch (sortBy)
{
case "name_desc":
students = students.OrderByDescending(s => s.Surname);
break;
case "Date":
students = students.OrderBy(s => s.StartDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.StartDate);
break;
default:
students = students.OrderBy(s => s.Surname);
break;
}
return View(db.Students.ToList());
}
我的看法
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.ActionLink("Surname", "Index", new { sortBy = ViewBag.NameSortParm})
</th>
<th>
@Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSort})
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.DisplayFor(modelItem => item.StartDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
这为我提供了视图中的操作链接,以及单击 localhost:5841/students?sortBy=Surname%20desc 和 localhost:5841/students?sortOrder=StartDate 后似乎正确的内容。我的问题是它们没有按应有的方式进行更改和排序。我错过了什么吗?
谢谢
【问题讨论】:
标签: asp.net-mvc-4