【问题标题】:Dropdown in every row of jquery datatablejquery数据表每一行的下拉菜单
【发布时间】:2025-12-23 05:15:12
【问题描述】:

我有 2 张桌子。 Category(Id, Name)Product(Id, Name, CatgeoryID)。现在我必须在数据表中显示产品名称和类别名称。我正在处理 jquery datatablea 并且需要从数据库中获取基于产品的类别。我理解这部分,在('#myDatatable').Datatable 列部分下,我必须创建一个下拉列表,但是如何创建?

【问题讨论】:

  • 到目前为止你有没有尝试过
  • 能否请您展示您的代码,如何将数据表与 Jquery 绑定。

标签: jquery model-view-controller datatable


【解决方案1】:

您可以使用 HTML 表格来实现这一点。在示例中,我通过元组类传递产品和类别

型号

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
}


public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int CatgeoryID { get; set; }
}

控制器

// Get the product and category data from the database
var tuple = new Tuple<List<Category>, List<Product>>(categories, products);
return View(tuple);

剃须刀

@model Tuple<List<Category>, List<Product>>

<table>
    @foreach (Product item in Model.Item2)
    {
    <tr>
        <td>@item.ID</td>
        <td>@item.Name</td>
        <td>
            @Html.DropDownList("Category",
                        new SelectList(Model.Item1,"ID", "Name", @item.CatgeoryID),
                        "Select Category",
                        new { @class = "form-control" })
            </td>
    </tr>
    }
</table>

【讨论】: