我正在使用orderby和orderbydescending列表函数进行排序,但只对readonly正确排序的字段进行排序。能帮我解决这个问题吗?感谢。
控制器代码:
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>
}
答案 0 :(得分:1)
这可能是您不能依赖Html.CheckBoxFor<T>()
而是应使用Html.CheckBox()
手动绑定您正在寻找的Ids
和值的实例。
这不是最好的,但它似乎对我有用。这是View
的伪代码示例,因为我在脑海里想着它:
@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> **CHANGE HERE**
@Html.CheckBox(string.format("On_Sale_{0}", Model.ProductItems[i].ProductID), Model.ProductItems[i].IsOnSale)
@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>
}
答案 1 :(得分:0)
我遇到了类似的问题,并且能够通过使用复选框,隐藏和小JQuery来获取复选框值,如下所示:
@Html.CheckBox("isPreferred", Model.IsPreferred)
@Html.HiddenFor(m => m.IsPreferred)
<script>
$("#isPreferred").change(function () {
$("#IsPreferred").val($("#isPreferred").val());
})
</script>