mvc3@Html.EditorFor和@Html.CheckBoxFor字段不绑定到新排序的值



我正在使用orderby和orderby降序列表函数进行排序,但只有只读字段才能正确排序。你能帮我解决这个问题吗。谢谢

控制器代码:

    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>
}

这可能是一个不能依赖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>
}

我遇到了一个类似的问题,并能够通过使用复选框、hiddenfor和类似的小JQuery来取回复选框值:

@Html.CheckBox("isPreferred", Model.IsPreferred)
@Html.HiddenFor(m => m.IsPreferred)
<script>
    $("#isPreferred").change(function () {
        $("#IsPreferred").val($("#isPreferred").val());
    })
</script>

相关内容

  • 没有找到相关文章