如何将 ID 参数传递给 DataTables 以限制基于该 ID 的结果

How to pass an ID parameter to DataTables to limit results based on that ID

提问人:Steven Showler 提问时间:10/10/2023 最后编辑:Steven Showler 更新时间:10/10/2023 访问量:19

问:

在我的应用程序中,我有一个 DataTable 集,它返回 Index 中的产品列表,以及一个用于导航 Products View 以显示所有相关信息的按钮。如图所示,我能够在基本表格中显示信息,但更愿意使用DataTables来增加功能。

在此视图中,我还希望显示存储它们的位置列表,但仅适用于该 ProductID。虽然我可以检索所有内容,并且如果我在调用中对 ProductID 进行硬编码 - 我无法从 View 传递它。

我的 GetAllStorageProducts 位于我的 ProductController 中:

[HttpGet]
        public IActionResult GetAllStorageProducts()
        {
            List<StorageProduct> productList = _unitOfWork.StorageProduct.GetAll(includeProperties:"StorageLocation,Product").ToList();
            return Json(new { data = productList });
        }

以及它调用 storageProduct.js 的脚本

var dataTable;
$(document).ready(function () {
    loadDataTable();
});

function loadDataTable() {
    dataTable = $('#tblData').DataTable({
        ajax: { url: '/Product/GetAllStorageProducts' },
        columns: [
            { data: 'storageLocation.storageLocationName', "width": "20%" },
            { data: 'localQtyOnHand', "width": "25%" }
        ]
    });
}

My View ion the ProductController (我的视图)

public IActionResult View(int? ProductId)
        {
            ProductVM productVM = new()
            {
                Product = new Product(),
                StorageProduct = _unitOfWork.StorageProduct.GetAll(includeProperties: "StorageLocation,Product")
                .Where(u => u.ProductId == ProductId).ToList(),
                ProductValues = _unitOfWork.ProductValues.GetAll(includeProperties: "StorageLocation,Product")
                .Where(u => u.ProductId == ProductId).ToList()
            };

            if (ProductId == null || ProductId == 0)
            {
                //CREATE
                return View(productVM);
            }
            else
            {
                //UPDATE
                productVM.Product = _unitOfWork.Product.Get(u => u.ProductId == ProductId);
                /*Will return the details about the product in JSON*/
                //return Json(new { data = productVM });
                return View(productVM);
            }
        }

产品虚拟机

    public class ProductVM
    {
        public Product Product { get; set; }
        public IEnumerable<StorageProduct> StorageProduct { get; set; }
        public IEnumerable<ProductValues> ProductValues { get; set; }
    }

这将返回:

enter image description here

但我想要的是:

enter image description here

我尝试更改 ProductVM 以包含 IActionResult 并传入 GetAllStorageProducts(productID),但未能奏效。我也尝试通过storageProduct.js传递参数,但这也不起作用。

这是可以实现的吗?

asp.net json ajax model-view-controller 数据表

评论


答: 暂无答案