0

我正在使用 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>
}
4

2 回答 2

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>
}
于 2013-07-16T05:11:27.493 回答
0

我遇到了类似的问题,并且能够通过使用复选框、hiddenfor 和 little JQuery 来获取复选框值,如下所示:

@Html.CheckBox("isPreferred", Model.IsPreferred)
@Html.HiddenFor(m => m.IsPreferred)

<script>

    $("#isPreferred").change(function () {

        $("#IsPreferred").val($("#isPreferred").val());

    })

</script>
于 2014-12-01T21:39:01.490 回答