【问题标题】:How can i select data from a Database with checkbox in Asp.net MVC?如何在 Asp.net MVC 中使用复选框从数据库中选择数据?
【发布时间】:2020-04-20 14:53:13
【问题描述】:

抱歉,我是 asp.net 和 MVC 的新手。 我有一个程序从数据库中读取数据并在索引页中显示结果,每行附近都有一个复选框:

<table class="table">
    <tr>
        <th>
            Select
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_Lot_Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_PDFToCreate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_PDFCreated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_Status)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rep_Type)
        </th>

    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.CheckBox("pdf", true)

            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_Lot_Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_PDFToCreate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_PDFCreated)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_Status)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rep_Type)
            </td>

        </tr>

    }

</table>

<script>
    function Obtain_Lot() {

        var name=...
    }
</script>

我的目标是创建一个按钮,当单击该按钮时,它会将检查的项目的“model.Rep_Lot_Id”作为字符串获取。只能检查一项。我该怎么做?

【问题讨论】:

  • 你可以使用jquery获取值为checked的复选框,然后使用jquery的travel函数获取Rep_Lot_Id
  • 谢谢,不知道旅行的功能。我会搜索它并尝试它是否有效。

标签: javascript asp.net asp.net-mvc model-view-controller


【解决方案1】:

我通常在你的情况下使用这个代码:

var checkedCheckbox = $("input:checked"); // consider only one checkbox can be checked
var repLotId = $(checkedCheckbox).parent().next().next().html(); // travel to the Rep_Lot_Id `td` and get its value.

【讨论】:

  • 谢谢,我已经尝试过了,但是当我在标签中打印 repLotId 进行测试时,它说变量未定义。
猜你喜欢
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 2020-11-23
  • 2018-03-29
  • 1970-01-01
  • 2020-03-31
  • 2016-05-30
  • 2016-03-02
相关资源
最近更新 更多