Reputation: 115
As Title, I would like to sort data table I want to sort from current datetime ---> pass datetime I'm a new in this programming please help :) thank you
Here is my code
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.SectorID)
</th>
.
.
.
<th>
@Html.DisplayNameFor(model => model.DateTime)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SectorID)
</td>
.
.
.
<td>
@Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
Upvotes: 1
Views: 9033
Reputation: 13765
just order the model in a new orderedCollection here is in your view
or you can do the somehing from your controller
View
@{
var modelOrdered = Model.OrderByDescending(m=>m.DateTime);
}
Controller
var model = context.collections.OrderByDescending(m => m.DateTime);
return View(model);
//and here just iterate
@foreach (var item in modelOrdered) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.SectorID)
</td>
.
.
.
<td>
@Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
Upvotes: 2
Reputation: 23935
The view is not the right place for manipulating data. You should do your ordering in your controller method, responsible for this view.
//this is where you fetch your data. Just add an .OrderBy or .OrderByDescending
var data = context.SomeEntity.OrderByDescending(ent => ent.DateTime);
return View(data);
Upvotes: 1