我正在使用 orderby 和 orderbydescending 列表函数进行排序,但只有只读字段才能正确排序。你能帮我解决这个问题吗?谢谢。
控制器代码:
public ActionResult ProductList()
{
var productList = new ProductListViewModel();
productList.ProductItems = productRepository.GetProductsWithDeleteOption().ToList();
productList.CategoryItems = categoryRepository.GetCategories().ToList();
var sortList = GetSortList();
productList.SortByList = sortList;
productList.SelectedSortByValue = "ProductID";
return View(productList);
}
[HttpPost]
public ActionResult ProductList(ProductListViewModel productViewModel, FormCollection formCollection, string submit)
{
if (ModelState.IsValid)
{
var sortList = GetSortList();
productViewModel.SortByList = sortList;
productViewModel.CategoryItems = categoryRepository.GetCategories().ToList();
productViewModel.SortByList = sortList;
// Check for submit action
if (submit == "Change Sort")
{
if (formCollection["SortDirection"] == "Desc")
{
switch (productViewModel.SelectedSortByValue)
{
case "ProductID": productViewModel.ProductItems = productViewModel.ProductItems.OrderByDescending(m => m.ProductID).ToList(); break;
case "Code": productViewModel.ProductItems = productViewModel.ProductItems.OrderByDescending(m => m.Code).ToList(); break;
case "Name": productViewModel.ProductItems = productViewModel.ProductItems.OrderByDescending(m => m.Name).ToList(); break;
case "Price": productViewModel.ProductItems = productViewModel.ProductItems.OrderByDescending(m => m.Price).ToList(); break;
default: productViewModel.ProductItems = productViewModel.ProductItems.OrderByDescending(m => m.ProductID).ToList(); break;
}
}
else
{
switch (productViewModel.SelectedSortByValue)
{
case "ProductID": productViewModel.ProductItems = productViewModel.ProductItems.OrderBy(m => m.ProductID).ToList(); break;
case "Code": productViewModel.ProductItems = productViewModel.ProductItems.OrderBy(m => m.Code).ToList(); break;
case "Name": productViewModel.ProductItems = productViewModel.ProductItems.OrderBy(m => m.Name).ToList(); break;
case "Price": productViewModel.ProductItems = productViewModel.ProductItems.OrderBy(m => m.Price).ToList(); break;
default: productViewModel.ProductItems = productViewModel.ProductItems.OrderBy(m => m.ProductID).ToList(); break;
}
}
}
else if (submit == "Delete all selected")
{
}
else if (submit == "Update All")
{
}
else if (submit == "Restrict Display")
{
}
}
return View(productViewModel);
}
看法:
@for (var i = 0; i < Model.ProductItems.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(m => m.ProductItems[i].ProductID)
@Html.ActionLink(Model.ProductItems[i].ProductID.ToString(), "ProductEdit", new { id = Model.ProductItems[i].ProductID })
</td>
<td>
@Html.HiddenFor(m => m.ProductItems[i].Code)
@Html.DisplayFor(m => m.ProductItems[i].Code)
</td>
<td>
@Html.HiddenFor(m => m.ProductItems[i].Name)
@Html.DisplayFor(m => m.ProductItems[i].Name)
</td>
<td>
@Html.EditorFor(m => m.ProductItems[i].Price)
</td>
<td>
@Html.CheckBoxFor(m => m.ProductItems[i].On_Sale, new { id = "On_Sale_" + Model.ProductItems[i].ProductID })
</td>
<td>
@Html.EditorFor(m => m.ProductItems[i].DiscountedPrice)
</td>
<td>
@Html.EditorFor(m => m.ProductItems[i].Weight)
</td>
<td>
@Html.CheckBoxFor(m => m.ProductItems[i].IsForDelete, new { id = Model.ProductItems[i].ProductID })
</td>
<td>
@Html.ActionLink("Edit", "ProductEdit", new { id = Model.ProductItems[i].ProductID }) |
@Html.ActionLink("Details", "Details", new { id = Model.ProductItems[i].ProductID }) |
@Html.ActionLink("Delete", "Delete", new { id = Model.ProductItems[i].ProductID })
</td>
</tr>
}