I have been having problems trying to implementing a sorting function for my project using the repository pattern. All the tutorials I have seen uses sorting via a database while I am simply just trying to sort some of my hard-coded objects that exist in my repository.
Object class
public class Object
{
[HiddenInput]
public int ObjectId { get: set; }
public string Name { get; set; }
}
IObjectRepository interface
public interface IObjectRepository
{
IEnumerable<Object> GetAllObjects();
//public List<Object> ObjectSort(string sortOrder);
}
ObjectRepository class
public class ObjectRepository : IObjectRepository
{
private List<Object> _objectList;
public ObjectRepository()
{
_objectList = new List<Object>()
{
new Object { ObjectId = 1, Name = "Math"},
new Object { ObjectId = 2, Name = "Science"}
};
}
public IEnumerable<Object> GetAllObjects()
{
return _objectList;
}
//public List<Object> ObjectSort(string sortOrder) method
}
Home Controller
public class HomeController : Controller
{
private readonly IObjectRepository _objectRepository;
public HomeController(IObjectRepository objectRepository)
{
_objectRepository = objectRepository;
}
public IActionResult Index()
{
return View();
}
public IActionResult ObjectList()
{
return View(_objectRepository.GetAllObjects());
}
public IActionResult Sort(string sortOrder)
{
ViewData["NameSortParm"] = String.IsNullorEmpty(sortOrder) ? "name_desc" : "";
var objects = from s in _objectRepository.GetAllObjects()
select s;
switch (sortOrder)
{
case "name_desc":
objects = objects.OrderByDescending(s => s.Name);
break;
default:
objects = objects.OrderBy(s => s.Name);
break;
}
return View("ObjectList");
}
}
The commented-out lines indicate that I am aware that something should go there but the tutorials I watched did not have anything in those models. I am attempting to sort by clicking on the table header for Name in my ObjectList View which more or less looks like this.
<th>
<a asp-action="ObjectList" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.Name)</a>
</th>
I am stuck and unsure about what to do. The farthest I have gotten is clicking on the table header for Name, but the page is unable to display the objects that I am trying to sort. Any help would be much appreciated.
You just need one ObjectList
action like below:
public IActionResult ObjectList(string sortOrder)
{
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
var objects = from s in _objectRepository.GetAllObjects()
select s;
switch (sortOrder)
{
case "name_desc":
objects = objects.OrderByDescending(s => s.Name);
break;
default:
objects = objects.OrderBy(s => s.Name);
break;
}
return View(objects);
}
View:
@model IEnumerable<MyObject>
@{
ViewData["Title"] = "ObjectList";
}
<h1>ObjectList</h1>
<table>
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ObjectId)
</th>
<th>
<a asp-action="ObjectList" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.Name)</a>
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.ObjectId)
</td>
<td>
@Html.DisplayFor(m => item.Name)
</td>
</tr>
}
</tbody>
</table>
Result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.